Chapter 7 正則表達式
1:定義RegExp對象:
1: var reCat = new RegExp(“cat”,”gi”);
2: var reCar = /Cat/gi
g是global的縮寫,
i是insensitive的縮寫(表示大小寫不敏感)
2: 使用RegExp對象:
RegExp對象的方法:test() 和 exec()
String 對象的方法:match(), search(), replace(), split()
――――――――――――――――――――――――――――――――――――
test 方法:
var sToMatch = "cat";
var reCat = /cat/;
alert(reCat.test(sToMatch)); //output true;
――――――――――――――――――――――――――――――――――――
exec方法:
返回一個字符串數組, 第一個條目是第一個匹配, 其它的都是反向引用
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()方法返回一個數組, 數組中的第一個題目是第一個匹配,
其它的都是反向引用, 在上面的例子中:
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方法
返回一個包含在字符串中所有匹配的數組
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方法
返回在字符串中出現的第一個匹配的位置
var sToMatch = "a bat, a cat, afat baT, a
faT cat";
var reAt = /at/gi;
alert(sToMatch.serach(reAt)); //output 3
――――――――――――――――――――――――――――――――――――
replace方法
用第二個參數來替換第一個參數,返回替換后的字符串
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”的所有出現, 必須指明/red/g
var reRed = /red/g;
alert(sToChange.replace(reRed,"blue"));
Result: The sky is blue, the sky
is blue.
Replace最強大的用法是接受一個函數作為第二個參數。這個函數可以接受一個參數(即 匹配正則表達式的文本),并返回替換文本。
――――――――――――――――――――――――――――――――――――
split方法
將字符串分割成一系列子串,并通過數組返回。
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: 正則表達式的組成部分
l 元字符
l 字符類
l
量詞
3.1 元字符:
( [ { \ ^ $ | ) ? * + . , 使用上述的元字符時,需要轉義
例如:
var reQMark = /\?/;
var reQMark = new RegExp("\\?");兩個反斜杠是因為: ”\”在字符串中本身需要轉義
3.2 字符類
簡單類:[abc] 匹配a或b或c
負向類:[^abc] 匹配除了a,b和c的所有字符
范圍類:[a-z] 匹配a到z
組合類:由上述幾種字符類組合而成, 內部的類之間不能有空格 [a-zA-Z0-9]
預定義類:
代碼 |
等同于 |
匹配 |
. |
[^\n\r] |
除了換行和回車之外的任意 |
\d |
[0-9] |
數字 |
\D |
[^0-9] |
非數字 |
\s |
[
\t\n\x0B\f\r] |
空白字符 |
\S |
[^
\t\n\x0B\f\r] |
非空白字符 |
\w |
[a-zA-Z0-9_] |
單詞字符(字母數字下劃線) |
\W |
[^a-zA-Z0-9_] |
非單詞字符 |
3.3 量詞
3.3.1簡單量詞:
代碼 |
描述 |
? |
出現0次或1次 |
* |
出現0次或多次 |
+ |
出現1次或多次 |
{n} |
一定出現n次 |
{n,} |
至少出現n次 |
{n,m} |
出現n次到m次 |
正則表達式 |
描述 |
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 貪婪的,惰性的和支配性的量詞
貪婪量詞:
先看整個字符串是否匹配;如果沒有,去掉最后一個字符;重復這個過程,一直到發現 匹配或者字符串不剩余任何字符。
惰性量詞:
先看字符串中的第一個字符是否匹配,
如果不, 則讀入下一個字符,重復一直到發現 匹配或者整個字符串都已經檢查過了。
支配性量詞:
只嘗試匹配整個字符串。(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: 分組
分組即用一系列的括號包圍一系列字符, 字符類以及量詞
示例: 輕松實現String的trim方法:
String.prototype.trim = function(){
var reExtraSpace = /^\s*(.*?)\s+$/;
return this.replace(reExtraSpace,"$1");
}
var sTest = "
hello, guy ";
alert(sTest.trim());
5: 反向引用
如果正則表達式中含有分組, 分組中的值將被保存。反向引用就是存儲在分組中的特殊值
例如表達式(a?(b?(c?)))將產生編號從1-3的三個反向引用:
(1): (A?(B?(C?)))
(2): (B?(C?))
(3): (C?)
獲取反向引用:RegExp.$1, RegExp.$2
在正則表達式中包含反向引用: /(dog)\1/ 同 /dogdog/
反向引用在replace方法中:$1 $2
6: 候選
var reRedOrBlack = /(red|black)/
7: 非捕獲性分組
不創建反向引用:(?:pattern)
var renumbers = /#(?:\d+)/;
8: 前瞻
正向前瞻檢查的是接下來出現的是不是某個特定的字符
負向前瞻檢查的是接下來的不應該出現的字符
JavaScript不支持負向前瞻
示例:
var sToMatch1 = "bedroom";
var reBed = /bed(?=room)/;
alert(reBed.test(sToMatch1)); //output true
9: 邊界
邊界 |
描述 |
^ |
行開頭 |
$ |
行結尾 |
\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));
日期的正則表達式:
^((((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-))$
加了時間驗證的
^((((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$