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.matc