gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:37:43 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com