麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Regex > 正文

關于IE的RegExp.exec的問題

2020-03-16 21:13:49
字體:
來源:轉載
供稿:網友
問題:"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: 



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: 









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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人黄色小视频网站 | 精品久久久久久久久久久久久久 | 一级毛片免费大片 | 亚洲第一精品在线 | 九色激情网 | 亚洲成人在线视频网 | 成人一级视频在线观看 | 中文字幕免费播放 | 亚洲精品一区二区三区在线看 | 久久最新免费视频 | 亚洲午夜精品视频 | 性 毛片| 欧美成人精品欧美一级乱黄 | 精品中文字幕久久久久四十五十骆 | 日韩色视频 | 久久久久久久久久91 | 香蕉久草在线 | 亚洲精品在线观看网站 | 草莓视频久久 | 男女羞羞视频在线观看免费 | 51色视频 | 欧美另类综合 | 欧美三级欧美成人高清www | 草草久久久 | 九九热九九热 | 国产午夜免费福利 | 亚洲第一页在线观看 | 久草在线播放视频 | av电影免费在线 | 久久毛片 | 激情综合婷婷久久 | 麻豆视频在线免费观看 | 九一免费国产 | av在线免费看网址 | 欧美 中文字幕 | 久久久久久久一区二区三区 | 久久精品国产一区二区电影 | chinese军人gay呻吟 | 久久在现视频 | 国产一区二区成人在线 | 国产乱淫av |