JavaScript RegExp 对象使用技巧

2025-05-30 22:29:33

1、RegExp 可以简单到一个字母的检测,也可以复杂到一些元素的样式的检测。所有的检测结果会反馈在网页的上面。

JavaScript RegExp 对象使用技巧

2、一个检测百字的案例。<html><body><script type="text/javascript">var patt1=new RegExp("百");document.write(patt1.test("百度经验人人爱,百度经验就是好"));</script></body></html>

JavaScript RegExp 对象使用技巧

3、英文也可以检测出来。<html><body><script type="text/javascript">var patt1=new RegExp("o");document.write(patt1.test("I love this book and you?"));</script></body></html>

JavaScript RegExp 对象使用技巧

4、exec() 这个代码就是返回一个变量相同值的作用。如果句中没有,这个值就是nulll。<html><body><script type="text/javascript">var patt1=new RegExp("e");document.write(patt1.exec("This is right"));</script></body></html>

JavaScript RegExp 对象使用技巧

5、另外一种用法就是用两个参数。RegExp("e","g")<html><body&爿讥旌护gt;<script type="text/javascript">var patt1=new RegExp("e","g");do{result=patt1.exec("bee and eggs");document.write(result);}while (result!=null)</script></body></html>

JavaScript RegExp 对象使用技巧

6、compile() 也是一个重要的检测代码,它在使用的时候 可以改变这个RegExp()结果。

JavaScript RegExp 对象使用技巧
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢