首页 > 编程语言 > 详细

Javascript中的内置对象:RegExp对象

时间:2015-07-13 13:43:42      阅读:108      评论:0      收藏:0      [点我收藏+]

一、定义RegExp

RegExp对象用于存储检索模式。创建RegExp对象的检索模式如下:

var myPattern=new RegExp(pattern,attributes);

(1) 参数pattern是一个字符串,指定正则表达式的模式或其它正则表达式

(2)参数attributrs是一个可选的字符串,包含属性“g”、“i”、“M”,分别用于指定全局匹配、区分大小写的匹配和多行匹配。

二、RegExp的方法

共有三个方法:test()、exec()、compile()

1、test():检索字符串中指定的值,返回true或false

example:

var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free")); 

2、exec():检索字符串中指定的值,但是是返回找到的值

example:

var patt1=new RegExp("e");

document.write(patt1.exec("The best things in life are free")); 

3、compile():用于改变regexp的检索模式

var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free"));

patt1.compile("d");

document.write(patt1.test("The best things in life are free"));

 

Javascript中的内置对象:RegExp对象

原文:http://www.cnblogs.com/YanYongSong/p/4642453.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!