有关IE的RegExp.exec的问题

代码如下:
复制代码 代码如下:

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为空.

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

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

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死循环RegExplastIndex没有得到更新

In some recent code, I'm using Javascript to parse through the result of an AJAX call, which happens to be ing a full HTML page. Yes, ideally, I'd have an AJAX call something usable like JSON, but in this the PHP back-end code had to re as is and the front-end adjust to handle the legacy HTML it ed.
I needed to grab a link (1 or more) from the ed 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 up a to represent the ed HTML, in this 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 pr 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 :<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 :
x
y
z
Our while loop using RegExp.exec _disibledevent=>复制代码 代码如下:

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 :<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 eresting lastIndex bug in IE with zero-length matches. Hopefully, this will save someone a headache when trying to debug using Javascript RegExp.exec.
Tags: 

延伸阅读

最新评论

发表评论