Regular Expression Wildcard Matching

前端 未结 9 1386
甜味超标
甜味超标 2020-12-15 04:13

I have a list of about 120 thousand english words (basically every word in the language).

I need a regular expression that would allow searching through these words

9条回答
  •  醉梦人生
    2020-12-15 05:06

    function matchWild(wild,name)
    {
        if (wild == '*') return true;
    
        wild = wild.replace(/\./g,'\\.');
        wild = wild.replace(/\?/g,'.');
        wild = wild.replace(/\\/g,'\\\\');  
        wild = wild.replace(/\//g,'\\/');
        wild = wild.replace(/\*/g,'(.+?)');
    
        var re = new RegExp(wild,'i');
        return re.test(name);
    }
    

提交回复
热议问题