通过js实现字符数组中字符串的模糊匹配功能

1.定义字符数组

var electric_param_name = ['功率因数','cos'];

2.定义字符数组中的字符串模糊匹配方法

功能:查找 word 字符串 中是否包含字符数组arr中的某一项;

var matchStrExit = function(word, arr) {
    // word为匹配值
    if(arr.find((s) => {
        return word.search(s) !== -1;
    })){
        return true;
    }
    return false;
};

3.方法调用

示例一:

console.log("ans: ", matchStrExit('这个字符串中有功率因数', electric_param_name));

返回 ans: true;

示例二: 

console.log("ans: ", matchStrExit('功率', electric_param_name));

返回 ans: false;

方法思想来源于:用js实现在数组中模糊查询某个字符串_js模糊匹配字符串_chenwenqqqq的博客-CSDN博客有一个由多个字符串组成的数组。实现模糊匹配https://blog.csdn.net/chenwenqqqq/article/details/126800034

猜你喜欢

转载自blog.csdn.net/weixin_43823060/article/details/131170921