window.οnlοad=function(){
//RegExp
var text='000-00-0000';
var pattern=/\d{3}-\d{2}-\d{3}/;
//alert(pattern.test(text));//true
if(pattern.test(text)){
console.log("ok");
};
var text='mom and dad and baby';
var pattern=/mom?/gi;
console.log(pattern.exec(text));
//match and search 方法就是将上边的顺序倒过来
var text='000-00-0000';
var pattern=/\d{3}-\d{2}-\d{3}/;
//alert(text.search(pattern));//true
if(pattern.test(text)){
console.log("ok");
};
var text='mom and dad and baby';
var pattern=/mom?/gi;
console.log(text.match(pattern));
//replace
var text='mom and dad and baby';
var pattern=/mom?/gi;
alert(text.replace("om","lio"));//把是替换成谁
console.log(text.replace(/d/g,"lio"));//替换所有
};
js 正则表达式一看就懂
最新推荐文章于 2022-06-08 10:00:57 发布