简体   繁体   English

正则表达式为大写所有单词,但不包括 '(撇号)

[英]Regex to Upper Case all words but exclude ' (apostrophe)

I'm trying to build a funct to UpperCase all the string words but I'm having a problem with the following:我正在尝试为所有字符串单词构建一个大写的函数,但我遇到了以下问题:

String.prototype.upper = function() {
    return this.replace(/[^a-zA-Z0-9]+(.)/g, chr => chr.toUpperCase())
 }


let str = "My uncle's car is red";
console.log(str.upper()) 


//My Uncle'S Car Is Red

I need to exclude the S from being UpperCased, after the apostrophe.在撇号之后,我需要将S排除在大写字母之外。

Any ideas how this can be done?任何想法如何做到这一点?

Thank you谢谢

I would change the Regex to \s+\w to search for a letter after spaces and/or tabs.我会将正则表达式更改为\s+\w以在空格和/或制表符之后搜索字母。

 const upper = (input) => input.replace(/\s+\w/g, x => x.toUpperCase()); console.log(upper("My uncle's car is red"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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