Javascript中的内置对象:RegExp对象

一、定义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"));
原文地址:https://www.cnblogs.com/YanYongSong/p/4642453.html