1

这是我的代码:

// 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 返回正面?

我只希望文本与其中一个关键字匹配,它会返回肯定的。

但目前不工作。需要帮助。谢谢。

谢谢。

4

1 回答 1

0

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

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

于 2014-09-17T03:00:42.423 回答