I built a Javascript function to make the first letter uppercase. My issue is that I have some words which are like \"name_something\" and what I want is \"Name Something\".
You can use [\W_]
to get rid of characters that are not alphanumeric. Where W
represent characters from a-z
, A-Z
and 0-9
var str = 'this $is a _test@#$%';
str = str.replace(/[\W_]+/g,' ');
console.log(str);
So, to make the words capitalise alongside the replace you can do,
var str = 'this $is a _test@#$%';
str = str.replace(/[\W_]+/g,' ');
var res = str.split(' ').map((s) => s.charAt(0).toUpperCase() + s.substr(1)).join(' ');
console.log(res);