关于IE的RegExp.exec的问题

所属分类: 网络编程 / 正则表达式 阅读数: 404
收藏 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().
更多精彩内容其他人还在看

正则表达式匹配 非XXX的行

问题:如何匹配"非:.+123.123.123.10.+ " 行
收藏 0 赞 0 分享

正则表达式不包含属性

一个标签里不包含某个属性 的 正则表达式的写法
收藏 0 赞 0 分享

ASP正则函数替换分页后的参数

在分页系统里面用到的把page后面得东西都给丢掉
收藏 0 赞 0 分享

asp match正则函数使用Matchs实例

asp matchs函数提供了对正则表达式匹配的只读属性的访问。一直都用这个函数,没想到本站竟然没有这类文章,汗一个,最近我会多加一些这样的文章
收藏 0 赞 0 分享

asp 图片正则 替换,替换前检查图片是不是本地地址的方法

这个图片正则先检查图片的地址,不是本地的则用本地的asp突破盗链,方便使用,注意是答chinaz的朋友问的一个问题
收藏 0 赞 0 分享

java正则表达式彻底研究

从J2SE1.4起Java增加了对正则表达式的支持就是java.util.regex包
收藏 0 赞 0 分享

正则表达式口诀 正则表达式学习工具

正则表达式口诀 + 常用的正则表达式 + 正则表达式学习工具+正则处理工具 正则是每个程序员绕不开的堡垒,只有把它攻下来。我觉得正则之所以难,第一难是需要记忆,第二难是要求具备抽象逻辑思维。
收藏 0 赞 0 分享

比较实用的正则表达式学习笔记

最近在学习正则,一些比较有用的东西怕忘记,记下来,比较乱,想一条记录一条
收藏 0 赞 0 分享

asp只采集网站可见文本的正则

它可以过虑Js 可以过滤 CSS 过滤HTML标识,只采集页面的可见文本。
收藏 0 赞 0 分享

asp.net常用正则表达式

比较常用的多种语言支持的正则整理收集
收藏 0 赞 0 分享
查看更多