RegExp 对象
来源:互联网 发布:mac怎么截图啊 编辑:程序博客网 时间:2024/06/05 17:49
RegExp 是正则表达式的缩写。
当您检索某个文本时,可以使用一种模式来描述要检索的内容。RegExp 就是这种模式。
简单的模式可以是一个单独的字符。
更复杂的模式包括了更多的字符,并可用于解析、格式检查、替换等等。
您可以规定字符串中的检索位置,以及要检索的字符类型,等等。
定义RegExp
RegExp 对象用于存储检索模式。
通过 new 关键词来定义 RegExp 对象
RegExp 对象有 3 个方法:test()、exec() 以及 compile()。
test() 方法检索字符串中的指定值。返回值是 true 或 false
例子:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
由于该字符串中存在字母 "e",以上代码的输出将是:true
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
例子1:
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
由于该字符串中存在字母 "e",以上代码的输出将是:e
例子2:
您可以向 RegExp 对象添加第二个参数,以设定检索。例如,如果需要找到所有某个字符的所有存在,则可以使用 "g" 参数 ("global")。
在使用 "g" 参数时,exec() 的工作原理如下:
找到第一个 "e",并存储其位置
如果再次运行 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)
由于这个字符串中 6 个 "e" 字母,代码的输出将是:eeeeeenull
compile()函数
compile() 方法用于改变 RegExp。
compile() 既可以改变检索模式,也可以添加或删除第二个参数。var patt1=new RegExp("e");
例子:
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"));
由于字符串中存在 "e",而没有 "d",以上代码的输出是:truefalse
- RegExp 对象
- RegExp对象
- RegExp对象
- RegExp 对象
- RegExp对象
- RegExp对象
- RegExp对象
- RegExp对象
- RegExp() 对象
- RegExp对象
- RegExp对象
- 正则表达式(RegExp)对象
- JavaScript RegExp 对象参考
- RegExp 对象的方法
- JavaScript RegExp 对象
- JS中的RegExp对象
- JavaScript RegExp 对象
- JavaScript的RegExp对象
- 如何让Visual Studio和web标准联姻
- 想创业,就别输不起!--leo看赢在中国第三季(7)-----尊重原版作者牌权。
- 使用VISUAL STUDIO 2008 BETA2 创建 SHAREPOINT工作流
- 服务端控件弹出新窗口
- chm文件无法打开的解决方法,chm电子书显示“此程序无法显示网页”的解决办法
- RegExp 对象
- Visual Studio 2008加入拼写检查器
- ごみ家電宝の山
- linux修改文件访问权限 : chmod :chown 应用实例
- Visual Studio 2008中的SQL数据库发布
- 短信开发时的7BIT转换的原理!
- Java String转换为int
- Visual Studio 2008+NClay小试牛刀
- 【数据加密】Java中3DES加密解密调用示例