前端实现关键字模糊查询记录
test方法
代码展示:
const reg = new RegExp(关键字); //输入关键字
const data=['steven','mike','alice']
for (let index = 0; index < data.length; index++) {
if (reg.test(data[index])) {
return data[index] //返回满足条件的数据
}
IndexOf()方法的记录
注释:indexOf() 方法对大小写敏感!
注释:如果要检索的字符串值没有出现,则该方法返回 -1。
示例:
var str=["Hello"," world", "welcome", "to", "the universe"];
var key='world';
var arr=[]
for (let index = 0; index < str.length; index++) {
if (str[index].indexOf(key)>=0) {
arr.push(str[index]);//返回满足条件的数据
}}