Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109184
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 423
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-15 11:55
文章分类

全部博文(40)

文章存档

2016年(36)

2015年(2)

2013年(2)

我的朋友

分类: JavaScript

2016-02-17 14:40:08

RegExp是正则表达式的缩写
RegExp对象有3个方法:test()、exec()、compile().
test()
test()方法检索字符串中的指定值,返回值是true或false
例子:
var patt1 = new RegExp("e");
document.write(patt1.test("the best things in life are free"));
结果:true
exec()
exec()方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回null
例子:
var patt1 = new RegExp("e");
document.write(patt1.exec("the best things in life are free"));
结果:
e
例子2:
RegExp对象添加第二个参数"g",在使用"g"参数时,exec()的工作原理如下:
1、找到第一个“e”,并存储其位置
2、如果再次运行exec(),则从存储的位置开始检索,并找到下一个"e",并存储其位置
var patt1=new RegExp("e","g");
do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)
结果:eeeeeenull
compile()

compile() 方法用于改变 RegExp。

compile() 既可以改变检索模式,也可以添加或删除第二个参数。


例子:
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"));

结果:truefalse
阅读(869) | 评论(0) | 转发(0) |
0

上一篇:使用$R()函数

下一篇:创建和存储 cookie

给主人留下些什么吧!~~