js 查找字符串中指定字符 模糊查询 不区分大小写

发布时间 2023-04-14 14:58:47作者: 黑白棋学弟
  var str = "hello  world ! hello  coder!" ;
  //查找‘HELLO’是否存在,找不到返回null
  var reg = new RegExp('HELLO','i') ;
  var isHas = str.match(reg);
  console.log(isHas); 
  //打印结果: ["hello", index: 0, input: "hello  world ! hello  coder!", groups: undefined]
var str="Hello world!";
//查找"Hello"
var patt=new RegExp('HELLO','i') ;
var result=patt.test(str);
console.log(result) // true

如上两种,用正则new RegExp,test/match查找可以实现,不区分大小写