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"));
相關文章
相關標籤/搜索