user515
user515

Reputation: 45

How to do the capitalization with strings in js?

I wrote one function it will take care of doing the capitalization of first name and last name. Now, if it has any special characters it was not making that word first letter as capital.

Example: When I give @TEST XYX it will convert this to @test Xyz but, I need @Test Xyz.

Below I'm attaching my code

camelCaseConvertion = function(str){
    var lowerCase = String(str).toLowerCase();
    return lowerCase.replace(/(^|\s)(\w)/g, function(text) {
      return text.toUpperCase();
    });
  };

Upvotes: 0

Views: 64

Answers (1)

Ronn Wilder
Ronn Wilder

Reputation: 1368

// you can put a check for non word characters

camelCaseConvertion = function(str){
    var lowerCase = String(str).toLowerCase();
    return lowerCase.replace(/(^|\s)[^\w]*(\w)/g, function(text) {
      return text.toUpperCase();
    });
  };

Upvotes: 1

Related Questions