[SOLVED] Javascript Capitalize first letter of each word ignore Contractions


I am trying to Capitalize the first letter of each word in a string. I found similar questions online but none seem to answer my question of ignoring Contractions like can’t, won’t, wasn’t.

This snippet of code works but it also capitalizes the letter after the apostrophe in the contraction.

var str = str.replace(/\b\w/g, w => w.toUpperCase())

If the string contains a contraction like can’t or won’t it will output Can’T or Won’T.

Is there a way to ignore apostrophes that are in the middle of a word? I still want to capitalize words that are separated by other punctuation. For example:

  • this_is_an_example -> This_Is_An_Example
  • this/is/an/example -> This/Is/An/Example
  • this,is,an,example -> This,Is,An,Example


You can use

const texts = ["this_can't_be_an_example", 'this/is/an/example', 'this,is,an,example']
for (const text of texts) {
  console.log(text, '=>', text.replace(/([\W_]|^)(\w)(?<!\w'\w)/g, (_, x,y) => `${x}${y.toUpperCase()}` ))

See the regex demo. Details:

  • ([\W_]|^) – Group 1 (x): a non-alphanumeric char or start of string
  • (\w) – Group 2 (y): a word char
  • (?<!\w'\w) – a negative lookbehind that makes sure the Group 2 value is not preceded with a word char and a '.

Answered By – Wiktor Stribi┼╝ew

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *