【JS学习笔记】正则表达式

晓石头的博客
邮箱:[email protected]

转载请注明出处,原文链接:http://blog.csdn.net/qiulanzhu/article/details/50663597


/*=========================正则表达式=========================*/
//创建正则表达式
//1.采用new运算符
var book1 = new RegExp('Book', 'ig');		//i:忽略大小写	g:全局匹配
//2.字面量方式
var book2 = /book/;

var str = "this is a book! realy book";
document.write(book1.test(str));
document.write(book1.exec(str));

//String对象中的正则表达式方法
//match
var apple = /apple/ig;
var strapp = 'this is a apple, that is apple too!';
document.write(strapp.match(apple));
document.write(strapp.match(apple).length);

//search
var boy = /boy/ig;
var strboy = 'this is a boy, that is boy too!';
document.write(strboy.search(boy));				//返回查找到的位置,没有查找到返回-1。ps:search无需g全局,因为找到就返回

//replace
var pattern = /box/ig;
var strbox = "this is box, that is box!";
document.write(strbox.replace(pattern, 'desk'));	//将box替换成desk
	
//split
var patstr = / /ig;
var myStr = 'this is box! that is box!';
document.write(myStr.split(patstr));				//用空格拆分成数组

//RegExp的静态属性
var patgoogle = /googl(e)/;
var googleStr = 'this is google!';
patgoogle.test(googleStr);

document.write(RegExp.input);
document.write(RegExp.leftContext);
document.write(RegExp.rightContext);
document.write(RegExp.lastMatch);
document.write(RegExp.lastParen);

//RegExp对象的实例属性
var patbook = /book/igm;
var bookStr = 'book, book, book!';

document.write(patbook.global);
document.write(patbook.ignoreCase);
document.write(patbook.multiline);
document.write(patbook.source);

document.write(patbook.lastIndex);		//下次的匹配位置
patbook.test(bookStr);
document.write(patbook.lastIndex);

patbook.lastIndex = 10;
document.write(patbook.lastIndex);

//
//使用点元字符
var pat = /g..gle/;					//.:表示匹配任意字符
var str = 'google';
document.write(pat.test(str));

//重复匹配
var pat1 = /go*gle/;				//o*:表示匹配任意多个o(包括0个)	
var str1 = 'gogle';
document.write(pat1.test(str1));

var pat2 = /go+gle/;				//o+:表示匹配至少一个o	
var str2 = 'gooogle';
document.write(pat2.test(str2));

var pat3 = /go?gle/;				//o?:表示匹配0或者1个o	
var str3 = 'ggle';
document.write(pat3.test(str3));

var pat4 = /go{2,4}gle/;			//o{2,4}:表示匹配2-4个o
var str4 = 'goooogle';
document.write(pat4.test(str4));

var pat5 = /go{2}gle/;				//o{2}:表示限定俩个o
var str5 = 'google';
document.write(pat5.test(str5));

var pat6 = /go{2,}gle/;				//o{2,}:表示俩个及其以上的o
var str6 = 'goooogle';
document.write(pat6.test(str6));	


//字符匹配
var mat = /[a-zA-Z0-9]oogle/;				//[a-z]:表示26个小写英文字符中的任意一个都匹配,[a-zA-Z0-9]:表示字母数字全匹配
var matstr = '3oogle';
document.write(mat.test(matstr));

var mat1 = /[^0-9]oogle/;					//[^0-9]:表示非0-9都匹配	
var matstr1 = 'qoogle';
document.write(mat1.test(matstr1));


//锚元字符匹配
var yuan = /^[a-z]oogle/;					//[]前的^表示行首匹配,[]里的^表示非
var yuanstr = 'ggoogle';
document.write(yuan.test(yuanstr));

var yuan = /^[a-z]oogle$/;					//[]前的^表示行首匹配,$尾匹配
var yuanstr = 'google1';
document.write(yuan.test(yuanstr));

var yuan1 = /\woogle/;						//\w表示数字字母下划线[0-9a-zA-A_]
var yuanstr1 = 'ggoogle';
document.write(yuan1.test(yuanstr1));		

var yuan2 = /\Woogle/;						//\W表示非数字字母下划线[^0-9a-zA-A_]
var yuanstr2 = 'ggoogle';
document.write(yuan2.test(yuanstr2));

var yuan3 = /\doogle/;						//\d表示数字[0-9]
var yuanstr3 = '2oogle';
document.write(yuan3.test(yuanstr3));

var yuan4 = /\Doogle/;						//\D表示非数字[^0-9]
var yuanstr4 = 'ggoogle';
document.write(yuan4.test(yuanstr4));


var space = /goo\s*gle/;					//\s表示匹配空格 制表符
var spacestr = 'goo 		gle';
document.write(space.test(spacestr));

var space1 = /google\b/;					//\b表示是否到达边界
var spacestr1 = 'google   end';
document.write(space1.test(spacestr1));

var space2 = /google|baidu|bing/;			//|表示或匹配
var spacestr2 = 'this is bing';
document.write(space2.test(spacestr2));

//分组
var div = /123(google){2,3}/;
var divstr = 'this is 123googlegoogle';
document.write(div.test(divstr));

var div1 = /8(.*)8/;						//.*表示任意字符
var divstr1 = 'this is 8google8';
document.write(div1.test(divstr1));
document.write(RegExp.$1);					//$1表示第一个分组的内容

var div2 = /8(.*)8/;						//.*表示任意字符
var divstr2 = 'this is 8google8';
document.write(divstr2.replace(div2, '<strong>$1</strong>'));	//加粗分组一

var div3 = /(.*)\s(.*)/;
var divstr3 = 'google baidu';
document.write(divstr3.replace(div3, '$2,$1') +'<br>');					//交换分组一和分组二的顺序

var div4 = /^([a-z]*)\s([0-9]{4})$/;		// /^(?:[a-z]*)\s(?:[0-9]{4})$/ 非捕获性分组,?:表示不会捕获这个分组
var divstr4 = 'google 2016';
document.write(div4.exec(divstr4));			//google 2016,google,2016 :数组0-表示匹配的全部,数组1-表示匹配的分组一, 数组2-表示匹配的分组二
							
//贪婪和惰性
var google = /8(.*?)8/g;											//加?,使用惰性;	加g,使用全局
var googlestr = '8google8 8google8 8google8 8google8';
document.write(googlestr.replace(google, '<strong>$1</strong>'));

var google1 = /8([^8]*)8/g;											//不匹配8
var googlestr1 = '8google8 8google8 8google8 8google8';
document.write(googlestr1.replace(google1, '<strong>$1</strong>'));

//前瞻性捕获
var google2 = /goo(?=gle)/;
var googlestr2 = 'google';
document.write(google2.exec(googlestr2));			//返回的是goo

//正在表达式用到的特殊符号,前面需要加转义符
var change = /\[\]/;
var changestr = '[]';
document.write(change.test(changestr));


//使用换行模式
var row = /^[0-9]+/gm;								//如果限定了行首匹配,则需要开启换行模式
var rowstr = '1.baidu\n2.google\n3.bing';
document.write(rowstr.replace(row, '#'));

//常用的正则
//检查邮政编码
var post = /[1-9][0-9]{5}/;							//6位数字,第一位不能是0
var poststr = '402207';
document.write(post.test(poststr));

//检测文件压缩包
//var com = /^[\w\-]+\.zip|rar|gz$/;
var com = /zip$|rar$|gz$/;
var comstr = 'abc-123.rar';
document.write(com.test(comstr));

//删除多余空格
var spastr = '   abc	123 	345  678';
var spa = /[\s]+/g;
document.write(spastr.replace(spa, ''));

//删除首尾空格
//方法一:依次去掉首尾
var headstr = '              goo    gle     ';
//alert('|' + headstr + '|');
var head = /^[\s]+/;
var headstr1 = headstr.replace(head, '');
//alert('|' + headstr1 + '|');
var tail = /[\s]+$/;
//alert('|' + headstr1.replace(tail, '') + '|');

//方法二:利用分组
var distr = '              goo    gle     ';
var di = /(^\s+)(.*?)(\s+$)/;				//加?,使用惰性
//alert(di.exec(distr)[2]);
//alert(distr.replace(di,'$2'));

//电子邮件验证
var mailstr = '[email protected]';
var mail = /^([\w_/./-]+)@([\w\-]+)\.([a-zA-Z]{2,4})$/;
document.write(mail.test(mailstr));


你可能感兴趣的:(JavaScript,正则表达式)