1

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

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

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

4

1 回答 1

3

您需要使用词边界搜索\b

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

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

于 2010-10-19T17:06:17.237 回答