I have the following JavaScript regex which capitalizes the first character of most words except some exceptions:
There are two issues with this regex that I have not resolved. The very first letter should always be capitalized. The second letter of every word should be lower case.
Here is an example of how it is working:
function capitalizeFirst(str) {
return str.replace(/(?!^)\b(?!(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}
console.log(capitalizeFirst('the fox JUMP off of THE street'));
Where it should be returning this: ('of' and 'the' should be lower case except if on first word). Also it should lower case the rest of the word:
The Fox Jump Off of the Street
I could not find an answer to this specific question during a search.
I was able to figure out the solution. This is an explanation of fix:
https://regex101.com/function capitalizeFirst(str) {
return str.toLowerCase()
.replace(/\b(?!(?!^)(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}
console.log(capitalizeFirst('the fox JUMP off of THE street'));