仅匹配unicode字母 [英] Match only unicode letters
问题描述
我有以下仅允许字母的正则表达式:
i have the following regex that allows only alphabets :
/[a-zA-Z]+/
a = "abcDF"
if (a.match(/[a-zA-Z]+/) == a){
//Match
}else{
//No Match
}
如何使用p {L}(通用语言-德语,英语等任何语言.)来实现此目的
How can I do this using p{L} (universal - any language like german, english etc.. )
我尝试了什么:
a.match(/[p{l}]+/)
a.match(/[\p{l}]+/)
a.match(/p{l}/)
a.match(/\p{l}/)
但所有字母a ="aB"均返回null
but all returned null for the letter a = "aB"
推荐答案
从ECMAScript 2018开始,JavaScript最终支持 Unicode属性本身会转义.
Starting with ECMAScript 2018, JavaScript finally supports Unicode property escapes natively.
对于较旧的版本,您要么需要自己定义所有相关的Unicode范围.或者,您可以将Steven Levithan的 XRegExp
包与Unicode附件一起使用,并利用其Unicode属性快捷键:
For older versions, you either need to define all the relevant Unicode ranges yourself. Or you can use Steven Levithan's XRegExp
package with Unicode add-ons and utilize its Unicode property shortcuts:
var regex = new XRegExp("^\\p{L}*$")
var a = "abcäöüéèê"
if (regex.test(a)) {
// Match
} else {
// No Match
}
这篇关于仅匹配unicode字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!