5

Need to capitalize the first letter of each word in a sentence, my regex expression however is also capitalizing the 'm' in I'm.

The full expression is this:

/(?:^\w|[A-Z]|\b\w)/g

The problem here (I think) is that \b\w will grab the first letter after a word boundary. I'm assuming that single quotes denote a word boundary therefore also capitalizing the m of I'm into I'M.

Can anyone help me change the expression to exclude the 'm' after the single quotes?

Thanks in advance.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
patko
  • 75
  • 6

1 Answers1

2

Finding a real word break in the middle of language might be a bit more
complicated than using regex word boundary's.

 ( \s* [\W_]* )           # (1), Not letters/numbers,
 ( [^\W_] )               # (2), Followed by letter/number
 (                        # (3 start)
      (?:                      # -----------
           \w                       # Letter/number or _
        |                         # or,
           [[:punct:]_-]            # Punctuation
           (?= [\w[:punct:]-] )     #  if followed by punctuation/letter/number or '-'
        |                         #or,
           [?.!]                    # (Add) Special word ending punctuation
      )*                       # ----------- 0 to many times
 )                        # (3 end)

var str = 'This "is the ,input _str,ng, the End ';
console.log(str);
console.log(str.replace(/(\s*[\W_]*)([^\W_])((?:\w|[[:punct:]_-](?=[\w[:punct:]-])|[?.!])*)/g, function( match, p1,p2,p3) {return p1 + p2.toUpperCase() + p3;}));
  • The Unicode version `\s*([^\pL\pN]*[\pL\pN](?:[\pL\pN_-]|\pP(?=[\pL\pN\pP_-])|[?.!])*)` –  Apr 23 '17 at 21:58