Javascript array string to regex literal

后端 未结 2 647
北海茫月
北海茫月 2021-01-21 20:13

I have a working example here: http://jsfiddle.net/R7KuK/

I\'ve tried to create an array containing full regular expressions with regex delimiters and set flags, but the

相关标签:
2条回答
  • 2021-01-21 20:13

    as stolen from here:

    Use the RegExp object constructor to create a regular expression from a string:

    var re = new RegExp("a|b", "i");
    // same as
    var re = /a|b/i;
    
    0 讨论(0)
  • 2021-01-21 20:36

    Try this:

    var regexSplit = regex.split( '/' );
    var realRegex = new RegExp( regexSplit[1], regexSplit[2] );
    

    Or better:

    var regexMatch = regex.match( /^\/(.*)\/([^\/]*)$/ );
    var realRegex = new RegExp( regexMatch[1], regexMatch[2] );
    

    Better cause if your regex contains '/', the first one will fail. ;)

    0 讨论(0)
提交回复
热议问题