javascript - Javascript 检查 unicode 字符串中的整个单词
问题描述
在 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);
}
解决方案
推荐阅读
- ios - ASTableView 跳过更新,因为加载尚未发生
- discord.py - 有没有办法在没有数据库的情况下记录命令的特定用户以备将来使用?
- c - C中相互依赖的结构
- r - 将代码转换为 SQL Server 时,R 中出现“未知或未初始化的列”错误
- javascript - 如何在 SQL Server 和 PHP 之间准确地传达真假?
- typescript - 将字典中的值转换为文字值
- lisp - 我需要在 Racket 中创建一个函数,从列表中删除彼此相似的元素
- python - 如何在没有 .py 扩展名的情况下运行 argparse python 程序?
- python - 解析存储在 Pandas 列中的字符串,将其一部分放到同一数据帧内的不同列中
- django - Keycloak 与 Python 的集成