最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于IE的RegExp.exec的問題

 更新時間:2010年03月29日 17:29:45   作者:  
問題:"A[B]C[D]E[F]G"將其分為兩個數(shù)組,分別是 ACEG 和 [B][D][F].
代碼如下:
復(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為空.

網(wǎng)上查不到資料,請各位指點一二.

查詢過程中得了個意外收獲
復(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=/\[\w\]/ig.exec(st))!=null)s2.push(arr[0]);
alert(s1);
alert(s2.join(""));

該寫法IE死循環(huán)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.
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • 正則表達式不包含屬性

    正則表達式不包含屬性

    一個標(biāo)簽里不包含某個屬性 的 正則表達式的寫法
    2008-07-07
  • 正則表達式詳細介紹(上)

    正則表達式詳細介紹(上)

    這篇文章主要介紹了正則表達式,正則表達式是由英文詞語regular expression翻譯過來的,就是符合某種規(guī)則的表達式。本文將會詳細的介紹正則表達式,需要的朋友可以參考下
    2015-10-10
  • ES9的新特性之正則表達式RegExp詳解

    ES9的新特性之正則表達式RegExp詳解

    這篇文章主要介紹了ES9的新特性之正則表達式RegExp詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • javascript 數(shù)字的正則表達式集合

    javascript 數(shù)字的正則表達式集合

    這里收集的是驗證數(shù)字的正則表達式集合,比較的全了,需要驗證數(shù)字的朋友很值得參考下。
    2010-04-04
  • 正則表達式匹配不包含某些字符串的技巧

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

    這篇文章主要介紹了正則表達式匹配不包含某些字符串的技巧,詳細分解了應(yīng)該怎么做和這么做的原因,需要的朋友可以參考下
    2014-07-07
  • 淺談?wù)齽t表達式(Regular Expression)

    淺談?wù)齽t表達式(Regular Expression)

    本文介紹了正則表達式的一些學(xué)習(xí)內(nèi)容,以及在Javascript、PHP下如何使用正則表達式
    2014-08-08
  • javascript正則表達式基礎(chǔ)篇

    javascript正則表達式基礎(chǔ)篇

    這篇文章主要是介紹javascript正則表達式的一些基礎(chǔ)知識,主要是介紹javascript的正則書寫方法與常用實例,需要的朋友可以參考下
    2013-02-02
  • ES2015 正則表達式新增特性

    ES2015 正則表達式新增特性

    ES2015 正則表達式新增特性,在原來正則表達式基礎(chǔ)上,ES2015增強了對四字節(jié)unicode字符的支持等功能
    2016-12-12
  • 詳解js正則表達式語法介紹

    詳解js正則表達式語法介紹

    本篇文章主要介紹了js正則表達式語法,詳細的介紹了js正則表達式的各種用法、規(guī)則等,有興趣的同學(xué)可以了解一下。
    2016-11-11
  • 密碼強度的正則表達式兩種方案JS總結(jié)篇

    密碼強度的正則表達式兩種方案JS總結(jié)篇

    本文給出了兩個密碼強度的正則表達式方案,一個簡單,一個更復(fù)雜和安全。并分別給出了兩個方案的解析和測試程序。對密碼強度正則表達式的兩種方案感興趣的朋友跟隨腳本之家一起看看吧
    2018-03-03

最新評論

宜宾市| 黑河市| 奉节县| 资中县| 安吉县| 巴东县| 井研县| 凤阳县| 原阳县| 南平市| 阆中市| 穆棱市| 错那县| 县级市| 陵水| 松江区| 东台市| 南安市| 诏安县| 东辽县| 新闻| 蒙自县| 荆州市| 比如县| 泗水县| 静安区| 五指山市| 南康市| 曲麻莱县| 德保县| 无为县| 大化| 合川市| 武清区| 淳安县| 芜湖市| 贺兰县| 竹北市| 桦川县| 巴彦淖尔市| 丰原市|