admin管理员组文章数量:1384222
I need a JavaScript method to allows me to convert the first letter of every word in a string to a capital letter like this: mike tyson wayne
to Mike Tyson Wayne
.
The method should also be able to convert the first word after an hyphen to capital letter like this: mike-tyson wayne
to Mike Tyson Wayne
.
The method should also be able to convert uppercase words to capitalize words like this: MIKE-TYSON WAYNE
to Mike Tyson Wayne
.
I have tried this method using regular expression gotten from another thread:
text.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase())
- (^\w{1}): match first char of string
- |: or
- (\s{1}\w{1}): match one char that came after one space
- g: match all
- match => match.toUpperCase(): replace with can take function, so; replace match with upper case match
It worked for converting the first letter of a string to capital letter and also works for the first letter of a string after space.
I added this expression that was supposed to cater for first letter of a string after space to the expression. The expression is (?:- |\d\. ).*
Combined with the first expression, it became:
text.replace(/(^\w{1})|(\s{1}\w{1})|(?:- |\d\. ).*/g, match => match.toUpperCase());
The problem is that the method above doesn't work for the strings after the dash but still works for the strings after space.
I need a JavaScript method to allows me to convert the first letter of every word in a string to a capital letter like this: mike tyson wayne
to Mike Tyson Wayne
.
The method should also be able to convert the first word after an hyphen to capital letter like this: mike-tyson wayne
to Mike Tyson Wayne
.
The method should also be able to convert uppercase words to capitalize words like this: MIKE-TYSON WAYNE
to Mike Tyson Wayne
.
I have tried this method using regular expression gotten from another thread:
text.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase())
- (^\w{1}): match first char of string
- |: or
- (\s{1}\w{1}): match one char that came after one space
- g: match all
- match => match.toUpperCase(): replace with can take function, so; replace match with upper case match
It worked for converting the first letter of a string to capital letter and also works for the first letter of a string after space.
I added this expression that was supposed to cater for first letter of a string after space to the expression. The expression is (?:- |\d\. ).*
Combined with the first expression, it became:
text.replace(/(^\w{1})|(\s{1}\w{1})|(?:- |\d\. ).*/g, match => match.toUpperCase());
The problem is that the method above doesn't work for the strings after the dash but still works for the strings after space.
Share Improve this question edited Apr 21, 2022 at 11:26 Peter Seliger 13.5k3 gold badges30 silver badges44 bronze badges asked Aug 6, 2021 at 7:44 IdrisIdris 5181 gold badge14 silver badges37 bronze badges 1- There is another approach based on word boundaries. – Peter Seliger Commented Aug 6, 2021 at 9:24
3 Answers
Reset to default 3\d
is a digit, so your expression matches a-
or a digit followed by.
...{1}
means one occurrence, which is the default anyways...
Try something like this:
/(^\w)|([-\s]\w)/g
A regex like ... /(?<=\b)\w/g
or /(?<=\b)\p{L}/gu
... which both use a positive lookbehind for word boundaries but the second variant making use of Unicode property escapes might be a more generic yet viable approach ...
console.log(
"mike-tyson wayne"
.replace((/(?<=\b)\w/g), match => match.toUpperCase())
);
console.log(
"MIKE-TYSON WAYNE"
.toLowerCase()
.replace((/(?<=\b)\p{L}/gu), match => match.toUpperCase())
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
let text = "UNIVERSITY OF CALIFORNIA-SAN DIEGO";
let pattern = /\w*/g;
let result = text.replace(pattern, function(word){
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
});
//result = University Of California-San Diego
本文标签:
版权声明:本文标题:regex - Javascript method to capitalize the first letter of every word and also every word after hyphen or dash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744454057a2606901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论