Javascript array string to regex literal

后端 未结 2 651
北海茫月
北海茫月 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: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. ;)

提交回复
热议问题