2

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 RegExp object parses given strings as strings, not as regular expressions.

  var regex = "/wolves/i"

vs.

  var regex = /wolves/i

My question is: How do I convert string-ed regex into an actual regular expression?


UPDATE: It wasn't until Felix King kindly explained to me that

var array = ["/wolves/i", "/Duck/"];

can safely become:

var array = [/wolves/i, /Duck/];
Community
  • 1
  • 1
User2121315
  • 283
  • 4
  • 14
  • 2
    Check http://stackoverflow.com/questions/4589587/javascript-regular-expression-string-to-regex-object and http://stackoverflow.com/questions/874709/converting-user-input-string-to-regular-expression – arunes Mar 07 '12 at 20:53
  • 6
    Why don't you create the array as `[/wolves/i, /Duck/]`? Why do you use strings at all? That seems to be an unnecessary complication to me. – Felix Kling Mar 07 '12 at 20:55
  • @FelixKling: I wasn't aware that was even possible, not to use the quotes. – User2121315 Mar 07 '12 at 20:57
  • How did you create regular expressions then? `/.../` denotes a regex literal. You must have been using `/.../.test(...)` or `str.match(/.../)` before. Anyways, here is some documentation: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions – Felix Kling Mar 07 '12 at 20:58
  • @FelixKling: Yes, I've been using `str.match(/.../)`, though I wasn't aware it was possible not to use strings in arrays, assuming regexp's could contain unescaped characters and fail; or I could be trying to match for a dot and accidentally close one array item. – User2121315 Mar 07 '12 at 21:02
  • But you see, in `str.match(/.../)` you are not using quotation marks either :) And you can put any value in an array: `[{foo: 42}, 5, /bar/g, "baz", function(x) { return x;}]`. If the parser cannot identify what you are adding to the array, it will throw a syntax error anyway. – Felix Kling Mar 07 '12 at 21:03
  • If you're creating the array in JS to begin with, an array of regex would be the way to go (either `/.../` or `new RegExp(...)`). Don't waste CPU time converting strings to regex when you could just use regex to begin with. However, if you wanted to generate the array server-side, you would be stuck doing the conversion anyway. – kitti Mar 07 '12 at 21:04
  • @FelixKling: Yes, but `str.match(/.../)` doesn't like arrays ): – User2121315 Mar 07 '12 at 21:05
  • If you mean you cannot pass an array to `str.match` then that's true because it expects a regular expression. You can iterate over the array though and pass each expression individually (like you do in your fiddle). – Felix Kling Mar 07 '12 at 21:06
  • @FelixKling: Noted the `[{foo: 42}, 5, /bar/g, "baz", function(x) { return x;}]` and `str.match(/.../)`'s bad attitude towards arrays. Thank you! – User2121315 Mar 07 '12 at 21:08
  • possible duplicate of [How do you pass a variable to a Regular Expression JavaScript?](http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript) – Eric Wendelin Mar 07 '12 at 21:17

2 Answers2

3

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. ;)

kitti
  • 14,663
  • 31
  • 49
0

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;
Community
  • 1
  • 1
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43