开发者

Why the following regex does not work in IE?

Why does the following javascript regex开发者_如何学C 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜