gpt4 book ai didi

javascript - 匹配文本中的多个关键字(Javascript)

转载 作者:行者123 更新时间:2023-11-28 07:57:31 25 4
gpt4 key购买 nike

这是我的代码:

// Matching multiple keywords in a text
// Case 1
var text = "Hello, My name is @Steve, I love @Bill, happy new year!";
var keywords = ["steve"];
var matching = text.toLowerCase().search([keywords]);
console.log(matching);
// return 19

// Case 2
var text = "Hello, My name is @Steve, I love @Bill, happy new year!";
var keywords = ["bill"];
var matching = text.toLowerCase().search([keywords]);
console.log(matching);
// return 34

// Case 3
var text = "Hello, My name is @Steve, I love @Bill, happy new year!";
var keywords = ["steve, bill"];
var matching = text.toLowerCase().search([keywords]);
console.log(matching);
// return -1

// Case 4
var text = "Hello, My name is @Steve, I love @Bill, happy new year!";
var keywords = ["steve", "bill"];
var matching = text.toLowerCase().search([keywords]);
console.log(matching);
// return -1

如果我遗漏了一些东西,导致案例 3 和案例 4 的结果呈阳性?

我只想如果文本与其中一个关键字匹配,它将返回正值

但目前无法工作。需要帮助。谢谢。

谢谢。

最佳答案

阅读the docs显示为搜索方法提供的参数被转换为正则表达式,导致 /steve,bill/ 精确搜索 “steve,bill”

您需要提供正确的正则表达式。

关于javascript - 匹配文本中的多个关键字(Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25881515/

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