I use this #(\s|^)([a-z0-9-_]+)#i
for capitalize every first letter every word, i want it also to capitalize the letter if it's after a special mark like a dash(-)
Now it shows:
This Is A Test For-stackoverflow
And i want this:
This Is A Test For-Stackoverflow
Any suggestions/samples for me?
I'am not a pro, so try to keep it simple for me to understand.
replace(/\b\w/g, c => c. toUpperCase()); The regular expression basically matches the first letter of each word within the given string and transforms only that letter to uppercase.
Title case, which capitalizes the first letter of certain key words. Sentence case, in which titles are capitalized like sentences. Initial case, where the first letter of every word is capitalized.
Explanation: Upper case capitalize the first letter of a sentence and leave all other letters as lowercase.
+1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well:
var re = /(\b[a-z](?!\s))/g; var s = "fort collins, croton-on-hudson, harper's ferry, coeur d'alene, o'fallon"; s = s.replace(re, function(x){return x.toUpperCase();}); console.log(s); // "Fort Collins, Croton-On-Hudson, Harper's Ferry, Coeur D'Alene, O'Fallon"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With