匹配文本中的多个关键字(Javascript)
Matching multiple keywords in a text (Javascript)
这是我的代码:
// 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"
。
您需要提供适当的正则表达式。
相关文章:
- 使用“;这个“;JavaScript原型方法中的关键字
- JavaScript 中的嵌套函数和 “this” 关键字
- 重写需要javascript中带有import关键字的语法
- Javascript 对象和 this 关键字
- 如何在不知道关键字的情况下访问javascript对象值
- 可以在Javascript中添加新的关键字吗
- javascript中的这个关键字
- Javascript关键字搜索子类Div值
- 在对象中用Javascript编写此关键字
- 使用Javascript根据URL中的关键字隐藏下拉选项
- Javascript 中的 const 关键字 scope
- '描述'javascript中的关键字
- 在javascript中绑定到此关键字
- 所以我正在使用Javascript const关键字,在IE中会发生什么
- 删除重复的关键字 JavaScript
- YouTube API v3 按关键字 JavaScript 搜索
- 基于关键字javascript组合2个数组
- 匹配文本中的多个关键字(Javascript)
- 从字符串中提取关键字:Javascript
- 向关键字javascript添加数组值