正则表达式编程
2014-04-01 10:55
218 查看
可以在 JScript 中使用正则表达式搜索字符串中的模式、替换文本以及提取子字符串。
搜索
以下 JScript 示例查找某个单词的所有匹配项。
创建该正则表达式的语句为
var re = /\w+/g;
/\w+/ 模式指定匹配以下一个或多个任意字符:A-Z、a-z、0-9 和下划线字符。 模式之后的 g(全局)标志,指示搜索操作应查找该模式的所有匹配项,而不仅仅是第一个匹配项。
还可以使用以下 JScript 替换语法。
var re = new RegExp("\\w+", "g");
要检索每个匹配项,exec 方法 将从 lastIndex 位置继续搜索,直到返回 Null。
JScript
以下示例仅查找第一个匹配项。 因为未设置全局 (g) 标志,搜索操作将从搜索字符串的起始位置开始。
JScript
替换
在以下示例中,“a”将替换“the”的匹配项。 但不会替换实例“The”,因为正则表达式标志中未包含 i(忽略大小写)标志。
该示例使用 replace 方法。
JScript
提取子字符串
在正则表达式模式中放置括号以创建可存储以供将来使用的子匹配项。
在以下示例中,该模式包含三个子匹配项。 子匹配字符串与每个匹配项一起显示。
exec 方法 将返回一个数组。 数组元素 0 包含了完整的匹配项,而元素 1 到 n 包含子匹配项。
JScript
Flags
在 JScript 正则表达式 /abc/gim 中,g 指示全局标志、i 指示忽略大小写标志,而 m 指示多行标志。
下表显示了允许的标志。
附加功能
还可以使用以下附加编程功能。
请参见
正则表达式语法
搜索
以下 JScript 示例查找某个单词的所有匹配项。
创建该正则表达式的语句为
var re = /\w+/g;
/\w+/ 模式指定匹配以下一个或多个任意字符:A-Z、a-z、0-9 和下划线字符。 模式之后的 g(全局)标志,指示搜索操作应查找该模式的所有匹配项,而不仅仅是第一个匹配项。
还可以使用以下 JScript 替换语法。
var re = new RegExp("\\w+", "g");
要检索每个匹配项,exec 方法 将从 lastIndex 位置继续搜索,直到返回 Null。
JScript
function SearchGlobal() { var src = "The quick brown fox jumps over the lazy dog."; // Create a regular expression pattern that has a global flag. var re = /\w+/g; var result; // Get the first match. result = re.exec(src); while (result != null) { print (result.index + "-" + result.lastIndex + "\t" + result[0]); // Get the next match. // Because the global flag is set, the search starts at the // position of lastIndex. result = re.exec(src); } // Output: // 0-3 The // 4-9 quick // 10-15 brown // 16-19 fox // 20-25 jumps // 26-30 over // 31-34 the // 35-39 lazy // 40-43 dog }
以下示例仅查找第一个匹配项。 因为未设置全局 (g) 标志,搜索操作将从搜索字符串的起始位置开始。
JScript
function SearchNonGlobal() { var src = "The quick brown fox jumps over the lazy dog."; // Create a regular expression that does not have // a global flag. var re = /\w+/; // Get the first match. // Because the global flag is not set, the search starts // from the beginning of the string. var result = re.exec(src); if (result == null) print ("not found"); else { print (result.index + "-" + result.lastIndex + "\t" + result[0]); } // Output: // 0-3 The }
替换
在以下示例中,“a”将替换“the”的匹配项。 但不会替换实例“The”,因为正则表达式标志中未包含 i(忽略大小写)标志。
该示例使用 replace 方法。
JScript
function ReplaceGlobal() { var src = "The batter hit the ball with the bat "; src += "and the fielder caught the ball with the glove."; // Replace "the" with "a". var re = /the/g; var result = src.replace(re, "a"); print(result); // Output: // The batter hit a ball with a bat and a fielder caught a ball with a glove. }
提取子字符串
在正则表达式模式中放置括号以创建可存储以供将来使用的子匹配项。
在以下示例中,该模式包含三个子匹配项。 子匹配字符串与每个匹配项一起显示。
exec 方法 将返回一个数组。 数组元素 0 包含了完整的匹配项,而元素 1 到 n 包含子匹配项。
JScript
function SearchWithSubmatches() { var result; var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"; // Create a regular expression to search for an e-mail address. // Include the global flag. // (More sophisticated RegExp patterns are available for // matching an e-mail address.) var re = /(\w+)@(\w+)\.(\w+)/g; // Get the first match. result = re.exec(src); while (result != null) { print ("e-mail address: " + result[0]); // Get the submatched parts of the address. print ("user name: " + result[1]); print ("host name: " + result[2]); print ("top-level domain: " + result[3]); print (""); // Get the next match. result = re.exec(src); } // Output: // e-mail address: george@contoso.com // user name: george // host name: contoso // top-level domain: com // e-mail address: someone@example.com // user name: someone // host name: example // top-level domain: com }
Flags
在 JScript 正则表达式 /abc/gim 中,g 指示全局标志、i 指示忽略大小写标志,而 m 指示多行标志。
下表显示了允许的标志。
JScript 标志 | 如果标志存在 |
---|---|
g | 查找搜索字符串中该模式的所有匹配项,而不仅仅是第一个匹配项。 |
i | 搜索不区分大小写。 |
m | ^ 匹配 \n 或 \r 之后的位置,而 $ 匹配 \n 或 \r 之前的位置。 无论标志是否存在,^ 均匹配搜索字符串开头的位置,而 $ 均匹配搜索字符串结尾的位置。 |
还可以使用以下附加编程功能。
功能 | 说明 |
---|---|
compile 方法 (Visual Studio - JScript) | 将正则表达式编译为内部格式,从而更快地执行。 |
test 方法 | 测试搜索字符串内是否存在模式。 |
search 方法 | 返回首个匹配项的位置。 |
参考
正则表达式对象概念
创建正则表达式正则表达式语法
相关文章推荐
- 通过添加字段实现级联删除
- 如何在eclipse jee中检出项目并转换为Maven project,最后转换为Dynamic web project
- [转]Delphi多线程编程入门(二)——通过调用API实现多线程
- [转]Delphi多线程编程入门(一)
- jsp页面 java 文件下载设置禁止IE直接打开配置问题
- ubuntu安装Kdevelop和qt4
- 获取java中Class类实例的三种方法
- C#事务处理
- C# winform 设置winform进入窗口后在文本框里的默认焦点
- spring定时器时间配置(转)
- 深入理解C语言
- C语言指针强制类型转换
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- F3-fatfree小型php框架教程(三)
- 为什么你写的Python运行的那么慢呢?
- 【小白笔记】PHP学习之路 (23) --对象 __set __get 类和对象的检测
- java 实现 图片上传
- 为啥TwitterFacebookgoogle亚马逊想变成对方?
- php面向对象之多态
- const与引用&