http://www.cnblogs.com/moqing/archive/2016/07/13/5665126.html 原文链接
一些补充
获取括号内的数字:
var str9 = '121321(123)321(456)(7)3123123';
var re =/\((\d+)\)/g;
cc=str9.match(re)
for(var i=0; i<cc.length;i++){
cc[i]=cc[i].slice(1,cc[i].length-1)
}
console.log(cc);
电话号码中间四位用*替换:
varstr6 ="15072132456 15272132456 19872132456 15072132456";
var reg = /(\d{3})\d{4}(\d{4})/g;
console.log(str6.replace(reg,'$1****$2'));
//打印结果 150****2456 152****2456 198****2456 150****2456
匹配单词至少4个:
var str = 'tomorrow is 121 anothor 32 qwerr dayw wwwwww ' ;
var re = /\b[a-zA-Z]{4,}\b/g;
匹配单词前后一致:
var str7 ="txt hello hight high bom";
var re = /\b([a-z])\w+\1\b/g;
str7.match(re) //打印结果txt hight high
日期格式化:
var str8 = '2017-12-30';
var re = /(\d+)-/g;
str8.replace(reg,'$1.')//打印结果2017.12.30
邮箱匹配:
var str7 ="7717878@qq.com";
var re =/^\w+@[a-z0-9]+(\.[a-z]+){1,3}$/;
str7.match(re)