富贵资源网 Design By www.hznty.com

废话不多说,直接步入正轨了,想要了解js正则表达式用法的相关知识,通过本教程学习吧。

定义和使用

var patt1 = new RegExp("hello"); 
var patt2 = /world/ ; 

test方法

test() 方法检索字符串中的指定值。返回值是 true 或 false。

var pat = /my/; 
var str = "this is my code..."; 
console.log(pat.test(str)); // true 

exec方法

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

var pat = /hello/; 
console.log(pat.exec("oh hello world")); //返还hello 

正则表达式类型

/pattern/attributes
参数 attributes 是一个可选的字符串,常用属性 “g”、”i” ,分别用于指定全局匹配、区分大小写的匹配。

var str = "Visit Hunger"; 
var patt1 = /hunger/i; //不区分大小写 
console.log(str.match(patt1)); //全局匹配 
var str="hello hunger valley! I am hunger"; 
var patt1=/hunger/g; 
console.log(str.match(patt1)); //不区分大小写,全局匹配 
var str="hello Hunger valley! I am hunger"; 
var patt1=/hunger/gi; 
console.log(str.match(patt1)); 

字符串正则

1. search

字符串查找

var str="Visit W3School!"; 
console.log(str.search(/w3school/)); //-1 console.log(str.serach(/w3school/i)); // 6 

2. match

字符串匹配

var str="1 plus 2 equal 33"; 
console.log(str.match(/\d+/)); //[1] 
console.log(str.match(/\d+/g)); //[1,2,33] 

3. replace

字符串替换

var str="Hello JI! oh I am hunger" 
console.log(str.replace(/Hunger/, "valley")); console.log(str.replace(/hunger/ig, "hunger")); 

4.split

字符串分割

var str = "Hello Hunger , oh I am Hunger"; 
str.split("");
str.split(/\s+/); 

正则写法

"htmlcode">

var str="Is this all there is"; 
var patt1=/[a-h]/g;
console.log(str.match(patt1)); 

"htmlcode">

var str="hello jikexueyuan!"; 
var patt1=/[^jike]/g; 
console.log(str.match(patt1)); 

"htmlcode">

var str="hello hunger! How are you"; 
var patt1=/hello|you/g; c
onsole.log(str.match(patt1)); 

"htmlcode">

var str="That's hot!"; 
var patt1=/h.t/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; 
var patt1=/\w/g; 
document.write(str.match(patt1));

"htmlcode">

var str="Give 100%!"; var patt1=/\W/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; 
var patt1=/\d/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; var patt1=/\D/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this all there is";
var patt1=/\s/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this all there is"; var patt1=/\S/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Hello jikexueyuan"; 
var patt1=/\bjikexueyuan/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Hello Hunger.\n be a FE."; 
var patt1=/\n/g; document.write(str.search(patt1)); 

"htmlcode">

var str="Hello HHunger! Hello World!"; 
var patt1=/H+/g; 
document.write(str.match(patt1)); 
var str="Hello Hunger! Hello World!"; 
var patt1=/\w+/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Hellooo Hunger! Hello World!"; var patt1=/lo*/g; document.write(str.match(patt1)) 

"htmlcode">

var str="1, 100 or 1000"; var patt1=/10"htmlcode">
var str="100, 1000 or 10000"; var patt1=/\d{4}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="100, 1000 or 10000"; var patt1=/\d{3,4}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="100, 1000 or 10000"; var patt1=/\d{3,}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this his"; var patt1=/is$/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this his"; var patt1=/^Is/g; document.write(str.match(patt1)); 

常见正则

汉字: [\u4e00-\u9fa5]
手机号: 1[0-9]{10}
邮箱: (\S)+[@]{1}(\S)+[.]{1}(\w)+

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com