日期:2014-05-16 浏览次数:20360 次
学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>
<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>
<script type="text/javascript"> var str = "This isn't me"; var reg = /is/; var ret = str.search(reg); alert(ret);//2 </script>
<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>
<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>
<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>