2

I am using Javascript to populate a contact form however one of the strings comes out in capitals due to how it is added, I.E SCHOOLNAME

<script type="text/javascript">
  var userName = '<ecom:PersonFirstName runat="server" /> <ecom:PersonLastName runat="server" />';
$('div.contact-form-name input').val(userName);

var school = '<acc:AccountName runat="server" />';
$('div.contact-form-school input').val(school);

</script>

Is it possible to change the case of the "school" to camel case no matter how it is originally found, SChool, school, School.

This should also apply if the name is "SCHOOL NUMBER ONE" should appear as "School Number One"

Hello World
  • 1,379
  • 4
  • 20
  • 41
  • 2
    That's not camel case - that's just capitalized. Camel case would be "schoolNumberOne". – chrisfrancis27 Aug 29 '12 at 13:01
  • How can you change **school** to camel-case? Camel-case applies to words which contain two words, like **camelCase** itself. – SexyBeast Aug 29 '12 at 13:01
  • 1
    Camel case isn't only camelCase CamelCase counts as well and I have looked around and can't see ways that would seem to work well, also I couldn't think of a better way to put one word names without calling it camel case without having to add a load more text than necessary, I think it's easy enough to tell what I meant. – Hello World Aug 29 '12 at 13:01
  • 3
    [This](http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) may be what you need. You want Title Case, not camelCase. – StoryTeller - Unslander Monica Aug 29 '12 at 13:03

2 Answers2

2

What you want to match and replace is:

  • First letter should be uppercase
  • Rest of that word should be lowercase

"Word" can be defined as non-space characters that have whitespace (or nothing) before them.

  • ^|\s+ means: start of string or whitespace
  • \S means: one non-whitespace character
  • \S* means: any amount of non-whitespace characters

By grouping them you can use the arguments passed to the function:

"scHOOL NUMBER ONE".replace(
  /(^|\s+)(\S)(\S*)/g,
  function(match, whitespace, firstLetter, rest) {
    return whitespace + firstLetter.toUpperCase() + rest.toLowerCase();
  }
);
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • Hey thank you, I have actually found another way of doing this that is tidier but I will choose this as an answer as it is helpful and I appreciate it. – Hello World Aug 29 '12 at 13:10
  • 1
    You can also use `\b` to match a word-boundary, which simplifies things a little: `str.replace(/\b(\S)(\S*)/g, function(match,first,rest){ return first.toUpperCase() + rest.toLowerCase(); });` – georgebrock Aug 29 '12 at 13:12
  • @HelloWorld If you found another way, why don't you post it as an answer. – epascarello Aug 29 '12 at 13:13
0

For reference, another way to do this.

                <script type="text/javascript">

                $(document).ready(function () {

                    String.prototype.toTitleCase = function () {
                        return this.replace(/\w\S*/g, function (text) {
                            return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
                        });
                    };

                var userName = 'BOB IS A PERSON';
                userName = userName.toTitleCase();
                $('div.contact-form-name input').val(userName);
             });

                </script>
Hello World
  • 1,379
  • 4
  • 20
  • 41