views:

47

answers:

2

Why does the following javascript regex works in Firefox but not in IE (tested on IE8).

myregexp = eval('/(?:^|;)\s*(\d+)\s*:[^;]*?megason[^;]*/gi');
myregexp.exec('0:QL12345ABC - MEGASONIAC BEST CAFE;'); //returns null in IE8
A: 

eval is not recommended, and there's no reason to use it in this case.

Also, I would look over this list:
http://www.javascriptkit.com/javatutors/redev3.shtml

To see if you want to use exec or not.

Kerry
+1  A: 

you have to add slashes:

myregexp = eval('/(?:^|;)\\s*(\\d+)\\s*:[^;]*?megason[^;]*/gi');

but as Kerry said, eval is not good on this context, use instead:

myregexp = /(?:^|;)\s*(\d+)\s*:[^;]*?megason[^;]*/gi;

or

myregexp = new RegExp('(?:^|;)\\s*(\\d+)\\s*:[^;]*?megason[^;]*','gi');
Lauri