首页前端开发其他前端知识关于IE的RegExp.exec的问题

关于IE的RegExp.exec的问题

时间2024-02-01 15:57:02发布访客分类其他前端知识浏览574
导读:收集整理的这篇文章主要介绍了关于IE的RegExp.exec的问题,觉得挺不错的,现在分享给大家,也给大家做个参考。 代码如下: 复制代码 代码如下: VAR st="A[B]C[D]E...
收集整理的这篇文章主要介绍了关于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为空.

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

查询过程中得了个意外收获
复制代码 代码如下:
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> \na href="y"> Y/a> \na 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> \na href="y"> Y/a> \na 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().

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

RegExp

若转载请注明出处: 关于IE的RegExp.exec的问题
本文地址: https://pptw.com/jishu/595609.html
javascipt 正则表达式英文版 javascript 数字的正则表达式集合

游客 回复需填写必要信息