Answer the question
In order to leave comments, you need to log in
How to use regular expressions correctly?
There is a task: Given a string. With the help of magic manipulations in JS, you need to make sure that all the first characters of the words are in upper case, and all the rest are in lower case. I feel with my butt that you can’t do without RegExp, I applied it. There is even an almost perfectly working code:
function titleCase(str) {
var str = str.toLowerCase().replace(/\b./g, function(m){
return m.toUpperCase();
});
return str;
}
titleCase("I'm a little tea pot");
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
"I'm"displays
I'M. How to fix?
Answer the question
In order to leave comments, you need to log in
function titleCase(str) {
var str = str.toLowerCase().replace(/\s[a-zA-Z]/g, function(m){
return m.toUpperCase();
});
return str[0].toUpperCase() + str.substr(1);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question