RegExp:正则表达式
用于进行检索内容
格式
var patt=new RegExp(pattern,modifiers);
pattern:检索的内容(表达式模型)
modifiers:修饰符(全局或大小写)
简化格式
var patt =/pattern/modifiers;
\w
用于查找单字符
注意转义规则
var patt =new RegExp("\\w");
等意于
var patt = /\w/;
修饰符
i:用于不区分大小写
g:用于执行全文搜索
i
var x = "hello World";
var patt = /world/i;
document.write(x.match(patt));
得到结果World
g
var str="Is this all there is?";
var patt1=/is/g;
document.write(str.match(patt1));
var patt1=/is/g;
document.write(str.match(patt1));
得到结果 is ,is
同时使用gi
var str="Is this all there is?";
var patt1=/is/gi;
var patt1=/is/gi;
document.write(str.match(patt1));
得到结果 Is,is,is
text()
测试是否含有制定的值,返回true or false
var
patt1=new
RegExp("e");
document.write(patt1.test("The
best things in life are free"));
返回 true
exec()
测试是否含有制定的值,返回该值或null
var
patt1=new
RegExp("e");
document.write(patt1.exec("The
best things in life are free"));
返回e
var
patt1=new
RegExp("m");
document.write(patt1.exec("The
best things in life are free"));
返回null