一 javascript正则表达式的基本知识
1 javascript 正则对象创建 和用法
声明javascript 正则表达式
var reCat = new RegExp("cat");
你也可以
var reCat = /cat/; //Perl 风格 (推荐)
2 学习最常用的 test exec match search replace split 6个方法
1) test 检查指定的字符串是否存在
var data = "123123";
var reCat = /123/gi;
alert(reCat.test(data)); //true
//检查字符是否存在 g 继续往下走 i 不区分大小写
2) exec 返回查询值
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/i;
alert(reCat.exec(data)); //Cat
3)match 得到查询数组
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
var arrMactches = data.match(reCat)
for (var i=0;i < arrMactches.length ; i++)
{
alert(arrMactches[i]); //Cat cat
}
4) search 返回搜索位置 类似于indexof
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert(data.search(reCat)); //23
5) replace 替换字符 利用正则替换
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert(data.replace(reCat,"libinqq"));
6)split 利用正则分割数组
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /\,/;
var arrdata = data.split(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
3 学习下 简单类 负向类 范围类 组合类
//简单类
var data = "1libinqq,2libinqq,3libinqq,4libinqq";
var reCat = /[123]libinqq/gi;
var arrdata = data.match(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]); // 1libinqq 2libinqq 3libinqq
}
//负向类
var data = "alibinqq,1libinqq,2libinqq,3libinqq,4libinqq"; //\u0062cf
var reCat = /[^a123]libinqq/gi;
var arrdata = data.match(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]); //4libinqq
}
//范围类
var data = "libinqq1,libinqq2,libinqq3,libinqq4,libinqq5"; //\u0062cf
var reCat = /libinqq[2-3]/gi;
var arrdata = data.match(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]); // libinqq2 libinqq3
}
//组合类
var data = "a,b,c,w,1,2,3,5"; //\u0062cf
var reCat = /[a-q1-4\n]/gi;
var arrdata = data.match(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]); // a b c 1 2 3
}
这些都是 js正则 最基本的使用方法, 看不会的请复制到笔记本练习下,看会了再往下面看。
二 javascript 正则表达式是分组知识
1) 简单分组
<script language="JavaScript">
<!--
var data ="Ah-mousemouse";
var reCat =/(mouse){2}/gi;
var arrdata = data.match(reCat);
for (var i =0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
//-->
</script>
2 复杂分组
<script language="JavaScript">
<!--
var data ="bb ba da bad dad aa ";
var reCat =/([bd]ad?)/gi; // 匹配出 ba da bad dad
var arrdata = data.match(reCat);
for (var i =0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
// 同时 也不介意将分组放在分组中间
// var re = /(mom( and dad)?)/; 匹配出 mom 或 mon and daa
//-->
</script>
3 反向引用
<script language="JavaScript">
<!--
var sToMatch ="#123456789";
var reNumbers =/#(\d+)/;
reNumbers.test(sToMatch);
alert(RegExp.$1);
var sToChange ="1234 5678";
var reMatch =/(\d{4}) (\d{4})/;
var sNew = sToChange.replace(reMatch,"$2 $1");
alert(sNew);
//-->
</script>
4 候选
<script language="JavaScript">
<!--
var sToMatch1 ="red";
var sToMatch2 ="black";
var reRed =/red/;
var reBlack =/black/;
alert(reRed.test(sToMatch1) reBlack.test(sToMatch1));
alert(reRed.test(sToMatch2) reBlack.test(sToMatch2));
var sToMatch1 ="red";
var sToMatch2 ="black";
var reRedOrBlack =/(red black)/;
alert(reRedOrBlack.test(sToMatch1));
alert(reRedOrBlack.test(sToMatch2));
//-->
</script>
5 非捕获性分组
<script language="JavaScript">
<!--
var sToMatch ="#123456789";
var reNumbers =/#(?:\d+)/;
reNumbers.test(sToMatch);
alert(RegExp.$1);
var sToMatch ="#123456789";
var reNumbers =/#(?:\d+)/;
alert(sToMatch.replace(reNumbers,"abcd$1"));
//-->
</script>
6前瞻
<script language="JavaScript">
<!--
var sToMatch1 ="bedroom";
var sToMatch2 ="bedding";
var reBed =/bed(?=room)/;
alert(reBed.test(sToMatch1)); //true
alert(reBed.test(sToMatch2)); //false
//负向前瞻
var sToMatch1 ="bedroom";
var sToMatch2 ="bedding";
var reBed =/bed(?!room)/;
alert(reBed.test(sToMatch1)); //false
alert(reBed.test(sToMatch2)); //true
//-->
</script>
7 边界
<script language="JavaScript">
<!--
var sToMatch ="Important word is the last one.";
var reLastWord =/(\w+)\.$/;
reLastWord.test(sToMatch);
alert(RegExp.$1); //one
var sToMatch ="Important word is the last one.";
var reLastWord =/^(\w+)/;
reLastWord.test(sToMatch);
alert(RegExp.$1); //Important
var sToMatch ="Important word is the last one.";
var reLastWord =/^(.+?)\b/;
reLastWord.test(sToMatch);
alert(RegExp.$1); //Important
var data =" First second thind fourth fifth sixth ";
var reCat =/\b(\S+?)\b/g;
var arrdata = data.match(reCat);
for (var i =0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
//-->
</script>
8 多行模式
<script language="JavaScript">
<!--
var data =" First second\n thind fourth\n fifth sixth";
var reCat =/(\w+)$/g;
var arrdata = data.match(reCat);
for (var i =0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
var data =" First second\n thind fourth\n fifth sixth";
var reCat =/(\w+)$/gm;
var arrdata = data.match(reCat);
for (var i =0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
//-->
</script>
至此结束,这些都是 javascript 正则表达式的基本方法,如果你看会了看复杂的正则你会有豁然开朗的感觉。
该贴被panpan.nie编辑于2015-4-8 14:44:34