我有以下只允许字母的正则表达式:
/[a-zA-Z]+/
a = "abcDF"
if (a.match(/[a-zA-Z]+/) == a){
//Match
}else{
//No Match
}
我如何使用 p{L}(通用 - 任何语言,如德语、英语等。)
我试过的:
a.match(/[p{l}]+/)
a.match(/[\p{l}]+/)
a.match(/p{l}/)
a.match(/\p{l}/)
但对于字母 a = "aB"全部返回 null
最佳答案
从ECMAScript 2018开始,JavaScript终于支持了Unicode property escapes天生的。
对于旧版本,您要么需要自己定义所有相关的 Unicode 范围。或者您可以使用 Steven Levithan 的 XRegExp
package with Unicode add-ons并利用其 Unicode 属性快捷方式:
var regex = new XRegExp("^\\p{L}*$")
var a = "abcäöüéèê"
if (regex.test(a)) {
// Match
} else {
// No Match
}
关于javascript - 仅匹配 unicode 字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210194/