当前位置:主页 > 网页前端 > JavaScript代码 >

JavaScript正则表达式小结(test|match|search|replace|split|exec)

时间:2021-01-04 16:13:44 | 栏目:JavaScript代码 | 点击:

学JavaScript正则的好文一篇:http://www.cainiao8.com/web/js_note/js_regular_expression.html

test:测试string是否包含有匹配结果,包含返回true,不包含返回false。

<script type="text/javascript"> 
 var str = "bbs.byr.cn"; 
 var reg = /b/; 
 var ret = reg.test(str); 
 alert(ret); //true 
</script> 

match:根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null

<script type="text/javascript"> 
 var str = "ThIS isn't me"; 
 var reg = /is/ig; 
 var ret = str.match(reg); 
 alert(ret); //IS, is 
 console.log(ret); //["IS", "is"] 
</script> 

 search :根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1

<script type="text/javascript"> 
 var str = "This isn't me"; 
 var reg = /is/; 
 var ret = str.search(reg); 
 alert(ret);//2 
</script> 

replace:根据pattern进行正则匹配,把匹配结果替换为replacement

<script type="text/javascript"> 
 var str = "i love china!"; 
 var pattern = /i/g; 
 var ret = str.replace(pattern, "I"); 
 alert(ret); //I love chIna! 
</script> 

split:根据pattern进行正则分割,返回一个分割的数组

<script type="text/javascript"> 
 var str = 'http://www.baidu.com/'; 
 var reg = /\W/; 
 var ret = str.split(reg); 
 console.log(ret); //["http", "", "", "www", "baidu", "com", ""] 
</script> 

exec:对string进行正则处理,并返回匹配结果.array[0]为原字符串,array[i]为匹配在整个被搜索字符串中的位置。

<script type="text/javascript"> 
 var str = "i love china!"; 
 var reg = /i\B/g; 
 var ret = reg.exec(str); 
 alert(ret); // i 
 console.log(ret); //["i", index: 9, input: "i love china!"] 
</script> 

您可能感兴趣的文章:

相关文章