JavaScript之正则表达式笔记一
预定义类:
[^\n\r] 除了换行和回车的任意字符
\d [0-9] 数字
\D [^0-9] 非数字
\s [ \t\n\x0B\f\r] 空白字符
\D [^ \t\n\x0B\f\r] 非空白字符
\w [a-zA-Z_0-9] 单词字符(所有字母、数字和下划线)
\W [^a-zA-Z_0-9] 非单词字符
量词:
? 出现零次或一次
* 出现零次或多次(任意次)
+ 出现一次或多次(至少出现一次)
{n} 一定出现n次
{n,m} 至少出现n次,但不能超过m次
{n,} 至少出现n次
贪婪+”?” –> 惰性+”+” –> 支配
贪婪性的量词优先匹配整个字符串,如若没有匹配成功则从字符串的末尾去掉一个字符,再继续匹配。
惰性的量词优先从字符串的第一个字符开始匹配,如若没有匹配成功则加入字符串第二个字符进行匹配。
支配量词只对字符串匹配一次,不做其他尝试。
贪婪到惰性到支配:
? —–> ?? —-> ?+ 出现零次或一次
* ——> *? ——> *+ 出现零次或多次(任意次)
+ ——-> +? ——> ++ 出现一次或多次(至少出现一次)
边界:
^ 行开头
$ 行结尾
\b 单词边界
\B 非单词边界
如下正则:
/****/igm 正则表达式末尾i代表忽略大小写. g代表匹配整个字符串global. m代表多行模式,匹配换行符和真正的结尾。
下面测试正则一些基础属性的例子–
var testType = 2; // 1为输出所有用例、2为输出最后一个用例
var result = new Array(); //保存测试用例结果
String.prototype.trim = function(){ //除去首尾的空白字符
var reExtraSpace = /^\s+(.*?)\s+$/; //^行头 $ 行尾 匹配出(.*?)中的部分
return this.replace(reExtraSpace,"$1");
}
//Example1:
var sToMatch = "cat";
var reCat = /cat/;
var arrMatches1 = reCat.test(sToMatch);
result.push(arrMatches1);
// true
//Example2:
var sToMatch2 = "a bat,a Cat,a fAt baT,a faT cat";
var reAt = /at/;
var arrMatches2 = reAt.exec(sToMatch2); //arrMatches2 包含符合匹配的一个项目
result.push(arrMatches2);
//at
//Example3:
var sToMatch3 = "a bat,a Cat,a fAt baT,a faT cat";
var reAt = /at/gi;
var arrMatches3 = sToMatch3.match(reAt); //arrMatches3为符合匹配的数组 g global i insensitive
result.push(arrMatches3);
//at,at,At,aT,aT,at
//Example4:
var sToMatch4 = "a bat,a Cat,a fAt baT,a faT cat";
var reAt = /at/gi;
var arrMatches4 = sToMatch4.search(reAt); //arrMatches4为符合匹配的项在对象中出现的第一个位置 gi标示无效
result.push(arrMatches4);
//3
//Example5:
var sToChange = "The Url is 0x32.cn";
var reUrl = /0x32.cn/;
var sStr1 = sToChange.replace(reUrl,"www.0x32.cn");
result.push(sStr1);
//The Url is www.0x32.cn
//Example6:
var sToChange2 = "The Url is 0x32.cn,Do you konw 0x32.cn? Oh!yes!";
var reUrl2 = /0x32.cn/g;
var sStr2 = sToChange2.replace(reUrl2,function(sMatch){
return "www.0x32.cn";
});
result.push(sStr2);
//The Url is www.0x32.cn
//Example7:
var sColor = "red,black,blue,pink,skyblue";
var reComma = /\,/;
var reColor = /blue/;
var arrColor=sColor.replace(reColor,"darkblue");
arrColor = arrColor.split(reComma);
result.push(arrColor);
//red,black,darkblue,pink,skyblue
//Example8:
var sInString = "e,you konw,\nI like this color,\naha";
var reStr3 = new RegExp("\\n","g");
var sStr3 = sInString.replace(reStr3,"");
result.push(sStr3);
//e,you konw,I like this color,aha
//Example9:简单类
var sToMatch9 = "a bat,a Cat,a fAt baT,a faT cat";
var reAt = /[bcf]at/gi;
var arrMatches9 = sToMatch9.match(reAt);
result.push(arrMatches9);
//bat,Cat,baT,faT,cat
//Example10:负向类
var sToMatch10 = "a bat,a Cat,a fAt baT,a faT cat";
var reAt = /[^cf]at/gi;
var arrMatches10 = sToMatch10.match(reAt);
result.push(arrMatches10);
//bat,baT
//Example11:范围类和组合类
var sToMatch11 = "Cat1,cat1,cat2,cat3,cat4,cat5,cat6,Cat8,Cat9,cat8,cat9,cat10";
var reAt = /cat[4-7]/g;
var arrMatches11 = sToMatch11.match(reAt);
result.push(arrMatches11);
//cat1,cat2,cat3,cat4
//Example12:量词
var sToMatch12 = "testted shared";
var reAt1 = /.*ed/g; //贪婪 testted shared
var reAt2 = /.*?ed/g; //惰性 testted,shared
//var reAt3 = /.*+ed/g; //支配 IE7 FireFox3.5.1 未测试成功 error: 错误的数量词
var arrMatches12 = sToMatch12.match(reAt2);
result.push(arrMatches12);
//testted shared
//Example13:反向引用
var sToMatch13 = " _this is trim test! ";
var arrMatches13 = sToMatch13.trim();
result.push(arrMatches13);
//_this is trim test!
//Example14:反向引用
var sToMatch14 = "1234 5678";
var reMatch14 = /(\d{4}) (\d{4})/;
var arrMatches14 = sToMatch14.replace(reMatch14,"$2 $1"); //$2:5678 $1:1234
result.push(arrMatches14);
//5678 1234
//Example15:反向引用 捕获性分组
var sToMatch15 = "let's fuck you bird!!!";
var reMatch15 = /fuck|bird/gi;
var arrMatches15 = sToMatch15.replace(reMatch15,function(sMatch){
return sMatch.replace(/./g,"*"); //保持单词字符个数的*
}); //$2:5678 $1:1234
result.push(arrMatches15);
//let's **** you ****!!!
//Example16:非捕获性分组 ?:
var sToMatch16 = "#123456789";
var reMatch16 = /#(?:\d+)/;
var arrMatches16 = reMatch16.test(sToMatch16);
result.push(arrMatches16);
//True
//Example17:非捕获性分组 去除HTML标签
var sToMatch17 = "Hello World!";
var reMatch17 = /<(?:.|\s)*?>/g;
var arrMatches17 = sToMatch17.replace(reMatch17,"");
result.push(arrMatches17);
//Hello World!
//Example18:前瞻 正向/负向
var sToMatch18 = "gowhere";
var sToMatch181 = "gothere";
var reMatch18 = /(go(?=where))/;
var reMatch181 = /(go(?!where))/;
var arrMatches18 = reMatch18.test(sToMatch18); //RegExp.$1 = go
var arrMatches181 = reMatch181.test(sToMatch181);
result.push(arrMatches18+"|"+arrMatches181);
//true | true
if(testType == 1){
for(var i =0; i < result.length ; i++){
alert("Example"+(i+1)+":\n----\n"+result[i]);
//window.close();
}
}else if(testType == 2){
alert("Example"+(result.length)+":\n----\n"+result[result.length-1]);
//window.close();
}
上面的代码是对regexp的一些基础进行练习测试的代码,还有诸如边界和多行模式就没有写出来了,在上面的文字和代码里已经提到了。
接下来在去深入一下regexp,去研究和学习一些比较常用到的Regexp Expressions,比如卡号、电话、邮箱、字符串等等。