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

时间:2014-09-17 02:52:38

标签: javascript arrays match matching keyword-search

这是我的代码:

// 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回归正面的事情?

我只想要文本与其中一个关键字匹配,它会返回正面

但此刻不行。帮助需要。感谢。

感谢。

1 个答案:

答案 0 :(得分:0)

阅读the docs表明,搜索方法提供的参数会转换为正则表达式,从而导致/steve,bill/完全搜索"steve,bill"

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