我有一个代表一段文字的字符串。
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/