婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 關(guān)于IE的RegExp.exec的問題

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

熱門標簽:惠安地圖標注 上海銷售電銷機器人軟件 自己做的電銷機器人 哈爾濱公司外呼系統(tǒng)代理 遼寧秒客來電話機器人 山東外呼系統(tǒng)聯(lián)系方式 地圖標注店鋪地圖標注酒店 浙江營銷外呼系統(tǒng)有哪些 淄博市張店區(qū)地圖標注
代碼如下:
復制代碼 代碼如下:

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)上查不到資料,請各位指點一二.

查詢過程中得了個意外收獲
復制代碼 代碼如下:

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.
復制代碼 代碼如下:

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

標簽:宣城 無錫 長沙 綿陽 泰州 銅川 重慶 西安

巨人網(wǎng)絡(luò)通訊聲明:本文標題《關(guān)于IE的RegExp.exec的問題》,本文關(guān)鍵詞  關(guān)于,的,RegExp.exec,問題,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《關(guān)于IE的RegExp.exec的問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于關(guān)于IE的RegExp.exec的問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 都昌县| 英山县| 天水市| 方正县| 龙里县| 手游| 武汉市| 溆浦县| 大港区| 高碑店市| 德化县| 沧源| 江口县| 马尔康县| 临武县| 华池县| 澳门| 清远市| 肃宁县| 南木林县| 湖口县| 三原县| 深州市| 军事| 黄龙县| 和平县| 荆州市| 枣强县| 敦化市| 读书| 合山市| 门头沟区| 区。| 建平县| 麻城市| 郸城县| 石屏县| 嵊州市| 杭锦后旗| 紫金县| 清新县|