在 Javascript 中检查 unicode 字符串中的整个单词的正确方法是什么。
这仅适用于 ASCII:
var strHasWord = function(word, str){
return str.match(new RegExp("\\b" + word + "\\b")) != null;
};
我按如下方式尝试了 XRegExp,但这也不起作用。
var strHasWord = function(word, str){
// look for separators/punctuation characters or if the word is the first or the last in the string
var re = XRegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)");
return re.test(str);
//return str.match(re);
}
有什么建议么。谢谢。
编辑 1 以下似乎可以解决问题。
var function strHasWord = function(word, str){
var re = RegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)", "u");
return str.match(re) != null;
//return re.test(str);
}