js 正則實例

1.匹配url參數正則表達式

var re = /([^&=]+)=?([^&]*)/g
while (r = re.exec("aaa1a=aabbbbbbb"))
{
alert(r);
}this

2.去重複url

var ss = "Is is the cost of of gasoline going up up?.\n";
var re = /\b([a-z]+) \1\b/gim; // 建立正則表達式樣式。
while (r = re.exec(ss))
{
alert(r);
}
var rv = ss.replace(re, "$1"); // 用一個單詞替代兩個單詞。
document.write(rv);spa

3.非捕獲組字符串

(?:X) (?=X) (?<=X) (?!X) (?<!X)it

var re = /(?:abc){2}/;
var str = "abcabc";
alert(re.test(str));
alert(RegExp.$1);test

?=n 量詞匹配任何其後緊接指定字符串 n 的字符串。im

var str = "Is this all there is";
var patt1 = /is(?= all)/;
document.write(str.match(patt1));樣式

var str = "Is this all there is";
var patt1 = /is(?! all)/gi;
document.write(str.match(patt1));while

相關文章
相關標籤/搜索