您的位置:首页 > 编程语言

正则表达式编程

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

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 方法

返回首个匹配项的位置。

请参见

参考

正则表达式对象

概念

创建正则表达式
正则表达式语法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: