关于IE的RegExp.exec的问题

所属分类: 网络编程 / 正则表达式 阅读数: 418
收藏 0 赞 0 分享
代码如下:
复制代码 代码如下:

var st="A[B]C[D]E[F]G";
var reg =/\[\w\]/ig;
var s1 = st.replace(reg,"");
var s2=[];
var arr;
while((arr=reg.exec(st))!=null)s2.push(arr[0]);
alert(s1);
alert(s2.join(""));


FF下正确显示,IE下S2为空.

网上查不到资料,请各位指点一二.

查询过程中得了个意外收获
复制代码 代码如下:

var st="A[B]C[D]E[F]G";
var reg =/\[\w\]/ig;
var s1 = st.replace(reg,"");
var s2=[];

var arr;
while((arr=/\[\w\]/ig.exec(st))!=null)s2.push(arr[0]);
alert(s1);
alert(s2.join(""));

该写法IE死循环RegExp的lastIndex没有得到更新

In some recent code, I'm using Javascript to parse through the result set of an AJAX call, which happens to be returning a full HTML page. Yes, ideally, I'd have an AJAX call return something usable like JSON, but in this case the PHP back-end code had to remain as is and the front-end adjust to handle the legacy HTML it returned.
I needed to grab a link (1 or more) from the returned HTML page so that I could immediately display those links in separate windows (each was a generated report). So, my first stab at this is shown in the following code example. Basically, we have setup a string to represent the returned HTML, in this case it contains 3 <a> links; and we want to use the standard Javascript RegExp object's exec() method to grab the URLS (href parameter) for each of those links. In our example, we just print them out in an unordered list to see what we've captured. The important lines of code we'll be looking at are highlighted in the example below.
复制代码 代码如下:

var s='<a href="x">X</a>\n<a href="y">Y</a>\n<a href="z">Z</a>\n';
document.write('Found the following link URLs in the string:<br/><ul>');
while (matches = /<a href=['"](.*)['"]>.*<\/a>/g.exec(s)) {
document.write('<li>' + matches[1] + '</li>\n');
}
document.write('</ul>');

Which, when run, we get the following results in Firefox/Safari/Chrome:
Found the following link URLs in the string:
x
y
z
Our while loop using RegExp.exec() on our in-line regular expression does what it's supposed to and continues to match from where it left off in the string giving us our captured portion in the matches[] array. However, when run in Internet Explorer, we get the following lovely result (at least up until IE tells us the script is no longer responding and asks us to kill it):
Found the following link URLs in the string:
x
x
x
x
x
x
x
x
x
…ad infinitum…
Obviously, we have generated an infinite loop using our code above in IE; but why? The issue is that IE doesn't correctly maintain the lastIndex member for the regular expression object each iteration through the loop. Each time through the loop, which if you look at the highlighted code is in-lined, IE creates a new RegExp object and hence resets the lastIndex member to the beginning of the string. Therefore, we match the first link in the string infinitely as the lastIndex pointer never progresses between matches. There is a way around this, and that is to declare the regular expression separately, outside the loop, (it gets created just once) and then call exec() on that singular RegExp object as follows:
复制代码 代码如下:

var rx = /<a href=['"](.*)['"]>.*<\/a>/g;
var s='<a href="x">X</a>\n<a href="y">Y</a>\n<a href="z">Z</a>\n';
document.write('Found the following link URLs in the string:<br/><ul>');
while (matches = rx.exec(s)) {
document.write('<li>' + matches[1] + '</li>\n');
}
document.write('</ul>');

Now, the lastIndex member of our RegExp object gets updated correctly and we get the results we expected. Somewhat related to this item is the following interesting lastIndex bug in IE with zero-length matches. Hopefully, this will save someone a headache when trying to debug using Javascript RegExp.exec().
更多精彩内容其他人还在看

手机号码验证方法(正则验证)

这篇文章主要介绍了手机号码验证方法(正则验证),在文章中还给大家补充了最新手机号的验证正则表达式,需要的朋友可以参考下
收藏 0 赞 0 分享

利用正则表达式提取固定字符之间的字符串

这篇文章主要给大家介绍了利用正则表达式提取固定字符之间的字符串,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

js中使用正则表达式查找字母和数字的方法

这篇文章主要介绍了 js中使用正则表达式查找字母和数字的方法,在代码底部给大家介绍了js用正则表达式验证密码包含数字和字母的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

一个容易犯错的js手机号码验证正则表达式(推荐)

这篇文章主要介绍了 一个容易犯错的js手机号码验证正则表达式(推荐),需要的朋友可以参考下
收藏 0 赞 0 分享

正则表达式进行页面表单验证功能

一般做到注册页面的时候,当用户填完信息,都需要对他们的信息进行验证,这就要用到正则表达式。本文通过实例给大家介绍正则表达式进行页面表单验证功能,一起看看吧
收藏 0 赞 0 分享

比较常用的几个正则表达式匹配数字(收藏)

正则表达式用于字符串处理、表单验证等场合,实用高效。今天小编给大家分享比较常用的几个正则表达式匹配数字,需要的朋友参考下
收藏 0 赞 0 分享

php与javascript正则匹配中文的方法分析

这篇文章主要介绍了php与javascript正则匹配中文的方法,结合实例形式分析了针对utf-8与GBK编码情况下的php、javascript正则匹配中文操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

bash 中用于grep的正则表达式

正则表达式是一类用于匹配文本的表达方式,常用于grep命令中表达检索条件。接下来通过本文给大家介绍bash 中用于grep的正则表达式,需要的朋友参考下吧
收藏 0 赞 0 分享

js中string之正则表达式replace方法详解

本篇文章主要介绍了js中string之正则表达式replace方法详解,replace方法是javascript涉及到正则表达式中较为复杂的一个方法,严格上说应该是string对象的方法。
收藏 0 赞 0 分享

常用证件号码的正则表达式大全(收集整理)

前段时间做一个项目,需要对各种常用证件进行验证。而港澳通行证,台湾通行证,护照这些证件,在网上并没有找到做正则验证的方法。后来从脚本之家网站的代码中发现了这些验证规则,特效分享给大家,供大家参考
收藏 0 赞 0 分享
查看更多