How to use just asterisk wildcard when searching?

时间秒杀一切 提交于 2019-12-25 09:17:04

问题


I want to just support asterisk(*) wildcard not regex.

This is my code :

for example : checkNames[i] = "Number of ongoing MS sessions"


var checkName = checkNames[i].split("*").join(".*");
supportRegExp(dataSource[j].ColumnName, checkName, validatedList,dataSource[j]);

and this is my supportRegExp function :

function supportRegExp(arrayElem, checkName, validatedList, elem) {
    var regexpArr = checkName
        .replace(/\t/g, '\n')
        .split("\n")
        .map(function (item) { 
            return "^" + item + "$"; 
        });
    var regexp = new RegExp(regexpArr, 'i');
    $my.isMatched = regexp.test(arrayElem)
    if ($my.isMatched) {
        validatedList.push(elem);  //elem: my object { inside ColumnName, DisplayName }
        AddValidatedList(arrayElem); //arrayElem : elem.ColumnName or elem.DisplayName bla bla.
    }
}

This is works. I am writing "num*" and coming result and then I am writing "num|m" and coming result. Because I am using regexp so I want to just support '*' sign.

For example : When I am writing num*, result should come but I am writing num|m should not come result. Because I want to just support asterisk sign.

How can I do ?

Any idea please.


回答1:


Borrowing heavily from this question, I think you are after something like the following:

var checkName =
    checkNames[i]
        .split("*")
        .map(function (s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); })
        .join(".*");

This escapes each part of the regular expression after splitting on the * character, ready to be joined back with .*.



来源:https://stackoverflow.com/questions/41298353/how-to-use-just-asterisk-wildcard-when-searching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!