0

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();
    });
  };
Dai
  • 141,631
  • 28
  • 261
  • 374
user515
  • 45
  • 7
  • "it will take care of doing the capitalization of first name and last name" — See [Falsehoods Programmers Believe About Names](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). If you are talking about names of people then you **will** introduce incorrect capitalization by doing this. For example, *Ronald McDonald* has a capital letter in the middle of his surname. – Quentin May 30 '18 at 11:43
  • Actually In my project this issue. Can you see the example and give any suggestion. – user515 May 30 '18 at 11:47
  • Possible duplicate of [Capitalize words in string](https://stackoverflow.com/questions/2332811/capitalize-words-in-string) – Ricardo Alvaro Lohmann May 30 '18 at 11:48
  • Possible duplicate of [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – Calvin Nunes May 30 '18 at 11:49
  • Quentin's comment is right. If despite this you want to know if a letter is "capitalizable" you can test if `char.toUpperCase() === char.toUpperCase()`. If `true`, it's not "capitalizable". Well instead of naming my son `null`, now i want to name him `@null` ;) – Kaddath May 30 '18 at 11:51

1 Answers1

1
// 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();
    });
  };
Ronn Wilder
  • 1,228
  • 9
  • 13