关于IE的RegExp.exec的问题

 更新时间:2010年03月29日 17:29:45   作者:  
问题:"A[B]C[D]E[F]G"将其分为两个数组,分别是 ACEG 和 [B][D][F].
代码如下:
复制代码 代码如下:

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().

相关文章

  • 正则表达式不包含属性

    正则表达式不包含属性

    一个标签里不包含某个属性 的 正则表达式的写法
    2008-07-07
  • 正则表达式详细介绍(上)

    正则表达式详细介绍(上)

    这篇文章主要介绍了正则表达式,正则表达式是由英文词语regular expression翻译过来的,就是符合某种规则的表达式。本文将会详细的介绍正则表达式,需要的朋友可以参考下
    2015-10-10
  • ES9的新特性之正则表达式RegExp详解

    ES9的新特性之正则表达式RegExp详解

    这篇文章主要介绍了ES9的新特性之正则表达式RegExp详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • javascript 数字的正则表达式集合

    javascript 数字的正则表达式集合

    这里收集的是验证数字的正则表达式集合,比较的全了,需要验证数字的朋友很值得参考下。
    2010-04-04
  • 正则表达式匹配不包含某些字符串的技巧

    正则表达式匹配不包含某些字符串的技巧

    这篇文章主要介绍了正则表达式匹配不包含某些字符串的技巧,详细分解了应该怎么做和这么做的原因,需要的朋友可以参考下
    2014-07-07
  • 浅谈正则表达式(Regular Expression)

    浅谈正则表达式(Regular Expression)

    本文介绍了正则表达式的一些学习内容,以及在Javascript、PHP下如何使用正则表达式
    2014-08-08
  • javascript正则表达式基础篇

    javascript正则表达式基础篇

    这篇文章主要是介绍javascript正则表达式的一些基础知识,主要是介绍javascript的正则书写方法与常用实例,需要的朋友可以参考下
    2013-02-02
  • ES2015 正则表达式新增特性

    ES2015 正则表达式新增特性

    ES2015 正则表达式新增特性,在原来正则表达式基础上,ES2015增强了对四字节unicode字符的支持等功能
    2016-12-12
  • 详解js正则表达式语法介绍

    详解js正则表达式语法介绍

    本篇文章主要介绍了js正则表达式语法,详细的介绍了js正则表达式的各种用法、规则等,有兴趣的同学可以了解一下。
    2016-11-11
  • 密码强度的正则表达式两种方案JS总结篇

    密码强度的正则表达式两种方案JS总结篇

    本文给出了两个密码强度的正则表达式方案,一个简单,一个更复杂和安全。并分别给出了两个方案的解析和测试程序。对密码强度正则表达式的两种方案感兴趣的朋友跟随脚本之家一起看看吧
    2018-03-03

最新评论