简体   繁体   English

正则表达式:在一些特殊字符后将非 ascii 字符大写

[英]Regex : capitalize non-ascii character after some special character

I tried this code to capitalize each non-ascii character or simple letter in a string after a space or a special character like "-", but it doesn't work :我尝试使用此代码将字符串中空格或特殊字符(如“-”)后的每个非 ascii 字符或简单字母大写,但它不起作用:

var re = /(\b[a-z](?!\s))/g;
var name = "jean-àris-sa ça";
name = name.replace(re, function(x){return x.toUpperCase();});
console.log(name) // Jean-àRis-Sa çà

Expected result would be "Jean-Àris-Sa Ça", how can I achieve that ?预期结果将是“Jean-Àris-Sa Ça”,我该如何实现?

You may use您可以使用

 console.log( "jean-àris-sa ça".replace(/(-|\\s+|^)(.)/g, function(_,$1,$2) { return $1 + $2.toUpperCase(); } ) )

The (-|\\s+|^)(.) pattern captures - or 1+ whitespaces (or just start of string position, an empty string) into Group 1 and any next char into Group 2 and returns the same string with that char in Group 2 turned into upper case. (-|\\s+|^)(.)模式将-或 1+ 个空格(或只是字符串位置的开始,一个空字符串)捕获到组 1 中,并将任何下一个字符捕获到组 2 中,并返回与该字符相同的字符串第 2 组变为大写。

As you may need to upper the first letter while lowering the rest of the letters in the same word, you may need a regex to match all letters.由于您可能需要在同一单词中降低第一个字母的同时降低其余字母,因此您可能需要一个正则表达式来匹配所有字母。 XRegExp library can work for you in all browsers: XRegExp库适用于所有浏览器:

 var regex = XRegExp("(\\\\pL)(\\\\pL*)"); console.log( XRegExp.replace("jean-àRiS-sa çA", regex, function(_, $1, $2) { return $1.toUpperCase() + $2.toLowerCase(); }, "all") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>

In the latest Chrome versions that support ECMAScript 2018 you may simply use在支持 ECMAScript 2018 的最新 Chrome 版本中,您可以简单地使用

 console.log( "jean-àRiS-sa çA".replace(/(\\p{L})(\\p{L}+)/gu, (_, $1, $2) => $1.toUpperCase() + $2.toLowerCase()) );

The \\pL or \\p{L} Unicode category matches any letter. \\pL\\p{L} Unicode 类别匹配任何字母。

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

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