javascript的exec()、test()、compile()用法
来源:互联网 发布:铭万网的合作伙伴网络 编辑:程序博客网 时间:2024/04/30 00:02
**(2)(重要)
exec():
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
例子:
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
由于该字符串中存在字母 "e",以上代码的输出将是:
e
小技巧:
您可以向 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
正则表达式:
(1)
test():
test() 方法检索字符串中的指定值。返回值是 true 或 false。
例子:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
因为有e
所以返回的是true
**(2)(重要)
exec():
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
例子:
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
由于该字符串中存在字母 "e",以上代码的输出将是:
e
小技巧:
您可以向 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
(3)
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"));
由于字符串中存在 "e",而没有 "d",以上代码的输出是:
truefalse
0 0
- javascript的exec()、test()、compile()用法
- 【JavaScript】(8)JavaScript中的正则表达式创建方式、基础语法、及其compile | exec | test 方法
- JavaScript RegExp 对象的3 个方法:test()、exec() 和 compile()
- JavaScript 正则表达式的test,exec,match
- test(),compile(),exec(),match(),search(),replace(),split()
- javascript正则表达式用法 (test|match|search|replace|split|exec)
- qt: exec()的用法,accept()和accepted的概念
- qt: exec()的用法,accept()和accepted的概念
- javascript中正则表达式的test()和exec()的区别
- JavaScript:exec()方法的用法及说明
- Js 正则表达式对象(方法:exec、test、match、search)
- exec族函数用法(转载)
- win7 vs2010 ogre1.72(1)cmake的问题compiler "cl" is not able to compile a simple test program.
- win7 vs2010 ogre1.72(1)cmake的问题compiler "cl" is not able to compile a simple test program.
- Python3 中compile_command/compile编译可执行语句(或文件)、parser剖析器创建可执行代码、exec/eval动态执行语句的深入分析
- python的exec, execfile,compile, eval
- JavaScript正则表达式exec和test方法
- python 中 exec、 eval、 execfile 和 compile 用法
- NYOJ-477 A+B Problem III
- 使用 DB2 UDB OLAP 函数
- C++11 新特性之 tuple
- duiLib : 修改DuiDesigner, 增强选择元素后的用户体验
- 画廊的应用2
- javascript的exec()、test()、compile()用法
- ODBC编程
- ubuntu下hbase的伪分布式安装与配置
- java编写的151个注意(3)
- javascript高内聚 松耦合
- jQuery getJSON在IE下不刷新,缓存的问题
- Python_生成测试数据
- javascript函数自传id用法
- 黑马程序员 银行调度系统