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

Matching multiple keywords in a text (Javascript)

本文关键字:关键字 Javascript 文本      更新时间:2023-09-26

这是我的代码:

// 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 返回正数的内容?

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

但此刻不工作。需要帮助。谢谢。

谢谢。

阅读文档显示,为搜索方法提供的参数被转换为正则表达式,从而导致/steve,bill/精确搜索"steve,bill"

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