简体   繁体   中英

Capitalizing first letter of each word, \b\w also applies to I'm

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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM