Chapter 7 正則表達(dá)式
1:定義RegExp對(duì)象:
1: var reCat = new RegExp(“cat”,”gi”);
2: var reCar = /Cat/gi
g是global的縮寫(xiě),
i是insensitive的縮寫(xiě)(表示大小寫(xiě)不敏感)
2: 使用RegExp對(duì)象:
RegExp對(duì)象的方法:test() 和 exec()
String 對(duì)象的方法:match(), search(), replace(), split()
――――――――――――――――――――――――――――――――――――
test 方法:
var sToMatch = "cat";
var reCat = /cat/;
alert(reCat.test(sToMatch)); //output true;
――――――――――――――――――――――――――――――――――――
exec方法:
返回一個(gè)字符串?dāng)?shù)組,
第一個(gè)條目是第一個(gè)匹配, 其它的都是反向引用
var sToMatch = "http://msdn.microsoft.com:80/scripting/default.htm";
var reS = /(\w+):\/\/([^:]+)(:\d*)?([^# ]*)/;
var arrMatches=reS.exec(sToMatch);
alert(arrMatches.length);
for(i=1;i<arrMatches.length;i++)
alert(arrMatches[i]);
exec()方法返回一個(gè)數(shù)組, 數(shù)組中的第一個(gè)題目是第一個(gè)匹配,
其它的都是反向引用, 在上面的例子中:
a[0]是匹配到的字符串,即:http://msdn.microsoft.com:80/scripting/default.htm
a[1]
包含
"http"
a[2]
包含
"msdn.microsoft.com"
a[3] 包含 ":80"
a[4] 包含 "/scripting/default.htm"
(也可以用RegExp.$1、RegExp.$2、RegExp.$3、RegExp.$4取值)
――――――――――――――――――――――――――――――――――――
match方法
返回一個(gè)包含在字符串中所有匹配的數(shù)組
var sToMatch = "a bat, a cat, afat baT, a
faT cat";
var reAt = /at/gi;
var arrMatches = sToMatch.match(reAt);
for(i = 1; i < arrMatches.length; i++)
alert(arrMatches[i]);
――――――――――――――――――――――――――――――――――――
search方法
返回在字符串中出現(xiàn)的第一個(gè)匹配的位置
var sToMatch = "a bat, a cat, afat baT, a
faT cat";
var reAt = /at/gi;
alert(sToMatch.serach(reAt)); //output 3
――――――――――――――――――――――――――――――――――――
replace方法
用第二個(gè)參數(shù)來(lái)替換第一個(gè)參數(shù),返回替換后的字符串
var sToChange = "The sky is red, the sky is red";
var reRed = /red/;
alert(sToChange.replace(reRed,"blue"));
Result: The sky is blue, the sky is red.
如果要替換“red”的所有出現(xiàn), 必須指明/red/g
var reRed = /red/g;
alert(sToChange.replace(reRed,"blue"));
Result: The sky is blue, the sky
is blue.
Replace最強(qiáng)大的用法是接受一個(gè)函數(shù)作為第二個(gè)參數(shù)。這個(gè)函數(shù)可以接受一個(gè)參數(shù)(即 匹配正則表達(dá)式的文本),并返回替換文本。
――――――――――――――――――――――――――――――――――――
split方法
將字符串分割成一系列子串,并通過(guò)數(shù)組返回。
var sColor = "red,blue,black,white,yellow,green";
var reComma = /\,/;
var arrColors = sColor.split(reComma);
for(i=1;i<arrColors.length;i++)
alert(arrColors[i]);
3: 正則表達(dá)式的組成部分
l
元字符
l
字符類
l
量詞
3.1 元字符:
( [
{ \ ^ $ | ) ? * + . , 使用上述的元字符時(shí),需要轉(zhuǎn)義
例如:
var reQMark = /\?/;
var reQMark = new RegExp("\\?");兩個(gè)反斜杠是因?yàn)椋?/span> ”\”在字符串中本身需要轉(zhuǎn)義
3.2 字符類
簡(jiǎn)單類:[abc] 匹配a或b或c
負(fù)向類:[^abc] 匹配除了a,b和c的所有字符
范圍類:[a-z] 匹配a到z
組合類:由上述幾種字符類組合而成, 內(nèi)部的類之間不能有空格 [a-zA-Z0-9]
預(yù)定義類:
代碼
|
等同于
|
匹配
|
.
|
[^\n\r]
|
除了換行和回車之外的任意
|
\d
|
[0-9]
|
數(shù)字
|
\D
|
[^0-9]
|
非數(shù)字
|
\s
|
[
\t\n\x0B\f\r]
|
空白字符
|
\S
|
[^
\t\n\x0B\f\r]
|
非空白字符
|
\w
|
[a-zA-Z0-9_]
|
單詞字符(字母數(shù)字下劃線)
|
\W
|
[^a-zA-Z0-9_]
|
非單詞字符
|
3.3 量詞
3.3.1簡(jiǎn)單量詞:
代碼
|
描述
|
?
|
出現(xiàn)0次或1次
|
*
|
出現(xiàn)0次或多次
|
+
|
出現(xiàn)1次或多次
|
{n}
|
一定出現(xiàn)n次
|
{n,}
|
至少出現(xiàn)n次
|
{n,m}
|
出現(xiàn)n次到m次
|
正則表達(dá)式
|
描述
|
ba?d
|
bd, bad
|
ba+d
|
bad, baad, baaad,…
|
ba*d
|
bd, bad, baad,
baaad,…
|
ba{1}d
|
bad
|
ba{1,}d
|
bad, baad, baaad,…
|
ba{1,2}d
|
bad, baad
|
3.3.2 貪婪的,惰性的和支配性的量詞
貪婪量詞:
先看整個(gè)字符串是否匹配;如果沒(méi)有,去掉最后一個(gè)字符;重復(fù)這個(gè)過(guò)程,一直到發(fā)現(xiàn) 匹配或者字符串不剩余任何字符。
惰性量詞:
先看字符串中的第一個(gè)字符是否匹配,
如果不, 則讀入下一個(gè)字符,重復(fù)一直到發(fā)現(xiàn) 匹配或者整個(gè)字符串都已經(jīng)檢查過(guò)了。
支配性量詞:
只嘗試匹配整個(gè)字符串。(IE不支持, Mozilla視為貪婪量詞處理)
貪婪
|
惰性
|
支配
|
?
|
??
|
?+
|
*
|
*?
|
*+
|
+
|
+?
|
++
|
{n}
|
{n}?
|
{n}+
|
{n,m}
|
{n,m}?
|
{n,m}+
|
{n,}
|
{n,}?
|
{n,}+
|
var sToMatch = "abbbaabbbaaabbb1234";
var re1 = /.*bbb/g;
var re2 = /.*?bbb/g;
//var re3 = /.*+bbb/g;
var arrRes1 = sToMatch.match(re1);
var arrRes2 = sToMatch.match(re2);
//var arrRes3 = sToMatch.match(re3);
alert(arrRes1); //output "abbbaabbbaaabbb"
alert(arrRes2); //output "abbb","aabbb","aaabbb"
4: 分組
分組即用一系列的括號(hào)包圍一系列字符,
字符類以及量詞
示例: 輕松實(shí)現(xiàn)String的trim方法:
String.prototype.trim = function(){
var reExtraSpace = /^\s*(.*?)\s+$/;
return this.replace(reExtraSpace,"$1");
}
var sTest = "
hello, guy ";
alert(sTest.trim());
5: 反向引用
如果正則表達(dá)式中含有分組, 分組中的值將被保存。反向引用就是存儲(chǔ)在分組中的特殊值
例如表達(dá)式(a?(b?(c?)))將產(chǎn)生編號(hào)從1-3的三個(gè)反向引用:
(1): (A?(B?(C?)))
(2):
(B?(C?))
(3):
(C?)
獲取反向引用:RegExp.$1, RegExp.$2
在正則表達(dá)式中包含反向引用: /(dog)\1/ 同 /dogdog/
反向引用在replace方法中:$1 $2
6: 候選
var
reRedOrBlack = /(red|black)/
7: 非捕獲性分組
不創(chuàng)建反向引用:(?:pattern)
var
renumbers = /#(?:\d+)/;
8: 前瞻
正向前瞻檢查的是接下來(lái)出現(xiàn)的是不是某個(gè)特定的字符
負(fù)向前瞻檢查的是接下來(lái)的不應(yīng)該出現(xiàn)的字符
JavaScript不支持負(fù)向前瞻
示例:
var sToMatch1 = "bedroom";
var reBed = /bed(?=room)/;
alert(reBed.test(sToMatch1)); //output true
9: 邊界
邊界
|
描述
|
^
|
行開(kāi)頭
|
$
|
行結(jié)尾
|
\b
|
單詞的邊界
|
\B
|
非單詞的邊界
|
10: 多行模式
var reMultiLine = /pattern/m;
var sToMatch = "First second" +
" third forth" +
" fifth sixth";
var reLastWord1 = /(\w+)$/g;
var reLastWord2 = /(\w+)&/gm;
var reBeginWord1 = /^(\w+)/g;
var reBeginWord2 = /^(\w+)/gm
alert(sToMatch.match(reLastWord1));
alert(sToMatch.match(reLastWord2));
alert(sToMatch.match(reBeginWord1));
alert(sToMatch.match(reBeginWord2));
日期的正則表達(dá)式:
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
加了時(shí)間驗(yàn)證的
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))
(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$