javascript - 使用 jQuery (Javascript) 在段落中查找 "word"索引

标签 javascript regex word-boundary

我有一个代表一段文字的字符串。

var paragraph = "It is important that the word cold is not partially selected where we search for the word old";

我希望能够在本段中搜索“单词”的索引,并让它对“单词”进行精确匹配。例如,搜索“old”时。我应该只得到 1 个结果,而不是 2 个,因为匹配“冷”中的“旧”是无效的。

最佳答案

需要使用分词搜索\b:

var idx = paragraph.search(/\bold\b/i);

这匹配分隔单词的空格、破折号等。

关于javascript - 使用 jQuery (Javascript) 在段落中查找 "word"索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3970774/

相关文章:

javascript - 我正在尝试使用 Microsoft 机器人框架构建聊天机器人

javascript - Jquery slideToggle 不能与 display none css 一起工作

Python - 使用 string.replace 转义双引号

html - 包含html数据的mysql表中的REGEX

bash - grep- 为什么反向引用周围必须有单词边界?

javascript - 单词边界对符号字符有效吗?

正则表达式单词边界无法识别标点符号

javascript - Angular : Write HTML Output to textfile

python - 在正则表达式匹配中插入字符串

javascript - $q.allPromises 返回一个数组,但我只想要一个元素,而不是全部