Javascript中的内置对象:RegExp对象

1、定义RegExp正则表达式

RegExp对象用于存储检索模式。建立RegExp对象的检索模式以下:regexp

var myPattern=new RegExp(pattern,attributes);对象

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

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

2、RegExp的方法it

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

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

example:方法

var patt1=new RegExp("e");

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

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

example:

var patt1=new RegExp("e");

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

三、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"));
相关文章
相关标签/搜索