Javascript正则表达式相当于/ [a-zA-Z] + /使用p {L} [英] Javascript regex equivalent of /[a-zA-Z]+/ using p{L}
问题描述
我有以下正则表达式只允许使用字母:
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
but all returned null for the letter a = "aB"
推荐答案
所有返回的null从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
}
这篇关于Javascript正则表达式相当于/ [a-zA-Z] + /使用p {L}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!