@thorncorona
2015-02-27T21:44:21.000000Z
字数 19000
阅读 1959
正则表达式
regular expression是一个描述字符模式的对象;
ECMAScript中的RegExp类表示正则表达式;
String和RegExp都定义了使用正则表达式进行强大的模式匹配、文本检索和替换的函数;
正则表达式主要用来验证客户端的输入数据;
类似于创建字符串,可以使用new运算符也可以采用字面值;
参数 | 含义 |
---|---|
g | 全局匹配 |
i | 忽略大小写 |
m | 多行匹配 |
下文中的javascript代码是通过 EclipseKepler 的 rhino 解释的
代码如下:
/**
* new运算符创建正则表达式
*/
var box1 = new RegExp('box'); //第一个参数是模式字符串
print('box1 = ' + box1);
var box2 = new RegExp('box', 'gim'); //第二个参数是模式修饰符
print('box2 = ' + box2);
/**
* 字面值创建正则表达式
*/
var box3 = /Box/; //字面值方式的正则表达式
print('box3 = ' + box3);
var box4 = /Box/ig; //在第二个斜杠后加上模式修饰符
print('box4 = ' + box4);
运行结果如下:
box1 = /box/
box2 = /box/gim
box3 = /Box/
box4 = /Box/gi
RegExp对象包含两个方法,test()和exec()
方法 | 功能 |
---|---|
test | 在字符串中测试模式匹配,返回true或false |
exec | 在字符串中执行匹配搜索,返回结果数组 |
代码如下:
/**
* new创建的正则表达式的test
*/
var pattern = new RegExp('box'); //验证test
var str1 = 'box';
var str2 = 'Box';
print('pattern.test(str1) = ' + pattern.test(str1));
print('pattern.test(str2) = ' + pattern.test(str2));
var pattern2 = new RegExp('box', 'i'); //不区分大小写
print('pattern2.test(str1) = ' + pattern2.test(str1));
print('pattern2.test(str2) = ' + pattern2.test(str2));
/**
* 使用一条语句实现的正则匹配
*/
var pattern3 = /box/i;
var str3 = 'This is a Box';
print("pattern3.test(str3) = " + pattern3.test(str3));
print("/box/i.test('This is a Box') = " + /box/i.test('This is a Box'));
/**
* exec方法
* 返回匹配到的字符串数组,如果没有就返回null
*/
var pattern4 = /box/i;
var ret1 = pattern4.exec(str3);
print("ret1 = " + ret1);
print("typeof ret1 = " + typeof ret1);
var str4 = 'ssss';
var ret2 = pattern4.exec(str4);
print("ret2 = " + ret2);
print("typeof ret2 = " + typeof ret2);
print("typeof null = " + typeof null);
运行结果如下:
pattern.test(str1) = true
pattern.test(str2) = false
pattern2.test(str1) = true
pattern2.test(str2) = true
pattern3.test(str3) = true
/box/i.test('This is a Box') = true
ret1 = Box
typeof ret1 = object
ret2 = null
typeof ret2 = object
typeof null = object
String对象提供了4个使用正则表达式的方法
方法 | 含义 |
---|---|
match(pattern) | 返回pattern中的子串或null |
search(pattern) | 返回字符串中pattern的开始位置 |
replace(pattern, replacement) | 用replacement替换pattern |
split(pattern) | 返回字符串按指定pattern拆分的数组 |
代码如下:
/**
* match方法
* 如果不开启全局就只返回匹配到的第一个字符串
* 如果开启全局就返回所有的被匹配字符串数组
*/
var pattern1 = /box/;
var pattern2 = /box/g; //开启全局
var str = 'This is a box, That is a box!';
print("str.match(pattern1) = " + str.match(pattern1));
var ret1 = str.match(pattern2);
print("ret1 = " + ret1);
print("ret1[0] = " + ret1[0]);
/**
* search方法
* 返回第一个匹配的位置,这里没有必要设置全局
* 找不到返回-1
*/
var pattern3 = /box/;
var ret2 = str.search(pattern3);
print("ret2 = " + ret2);
print("'This is a BOX!'.search(pattern3) = " + 'This is a BOX!'.search(pattern3));
/**
* replace方法
* 需要设置全局才会替换所有匹配到的字符串
*/
var str2 = 'This is a box, That is a Box!';
var pattern4 = /box/i;
var pattern5 = /box/ig;
var replacement = 'tom';
var ret3 = str.replace(pattern4, replacement);
var ret4 = str.replace(pattern5, replacement);
print("ret3 = " + ret3);
print("ret4 = " + ret4);
/**
* split方法
*/
var str3 = 'This is a box! That is a Box!';
var pattern6 = /!/g;
var pattern7 = / /g;
var ret5 = str3.split(pattern6);
var ret6 = str3.split(pattern7);
print("ret5 = " + ret5);
print("ret5.length = " + ret5.length);
print("ret6 = " + ret6);
print("ret6.length = " + ret6.length);
运行结果如下:
str.match(pattern1) = box
ret1 = box,box
ret1[0] = box
ret2 = 10
'This is a BOX!'.search(pattern3) = -1
ret3 = This is a tom, That is a box!
ret4 = This is a tom, That is a tom!
ret5 = This is a box, That is a Box,
ret5.length = 3
ret6 = This,is,a,box!,That,is,a,Box!
ret6.length = 8
属性 | 短名 | 含义 |
---|---|---|
input | $_ | 当前被匹配的字符串 |
lastMatch | $& | 最后一个匹配字符串 |
lastParen | $+ | 最后一对圆括号内的匹配子串 |
leftContext | $` | 最后一次匹配前的子串 |
rightContext | $' | 上次匹配之后的子串 |
multiline | $* | 用于指定是否所有的表达式都用于多行的布尔值 |
代码如下:
/**
* 正则表达式静态属性
*/
print("RegExp.input = " + RegExp.input);
var pattern = /google/i;
var str = 'This is a google! That is a GOOGLE!';
pattern.test(str); //必须执行一下,静态属性才有效
//需要在浏览器中使用alert运行才能打印出当前的匹配字符串
print("RegExp.input = " + RegExp.input);
print("RegExp.leftContext = " + RegExp.leftContext);
print("RegExp.rightContext = " + RegExp.rightContext);
print("RegExp.lastMatch = " + RegExp.lastMatch);
var pattern2 = /(g)oogle/i;
var str2 = 'This is a gogle! That is a GOOGLE!';
pattern2.test(str2);
print("RegExp.lastParen = " + RegExp.lastParen); //会打印匹配的在圆括号里字符串
print("RegExp.multiline = " + RegExp.multiline);
/**
* 所有的属性可以通过短名来操作
*/
var pattern3 = /google/i;
var str3 = 'This is a google! That is a GOOGLE!';
pattern3.test(str3);
print("RegExp['$&'] = " + RegExp['$&']);
print("RegExp.$_ = " + RegExp.$_);
运行结果如下:
RegExp.input =
RegExp.input =
RegExp.leftContext = This is a
RegExp.rightContext = ! That is a GOOGLE!
RegExp.lastMatch = google
RegExp.lastParen = G
RegExp.multiline = false
RegExp['$&'] = google
RegExp.$_ =
属性 | 含义 |
---|---|
global | 全局是否设置 |
ignoreCase | 忽略大小写是否设置 |
multiline | 多行是否设置 |
source | 正则表达式的源字符串 |
lastIndex | 下次匹配将从字符串的哪里开始 |
代码如下:
/**
* 正则表达式实例属性
*/
var pattern1 = /google/;
var pattern2 = /google/g;
var pattern3 = /google/ig;
var pattern4 = /google/igm;
print("pattern1.global = " + pattern1.global);
print("pattern2.global = " + pattern2.global);
print("pattern2.ignoreCase = " + pattern2.ignoreCase);
print("pattern3.ignoreCase = " + pattern3.ignoreCase);
print("pattern3.ignoreCase = " + pattern3.multiline);
print("pattern4.ignoreCase = " + pattern4.multiline);
print("pattern4.source = " + pattern4.source);
/**
* lastIndex动态属性
*/
var str = 'google google google';
print("pattern2.lastIndex = " + pattern2.lastIndex);
for(var i = 6; --i != 0; ){
pattern2.test(str);
print("pattern2.lastIndex = " + pattern2.lastIndex);
}
pattern2.lastIndex = 100;
print("pattern2.lastIndex = " + pattern2.lastIndex);
运行结果如下:
pattern1.global = false
pattern2.global = true
pattern2.ignoreCase = false
pattern3.ignoreCase = true
pattern3.ignoreCase = false
pattern4.ignoreCase = true
pattern4.source = google
pattern2.lastIndex = 0
pattern2.lastIndex = 6
pattern2.lastIndex = 13
pattern2.lastIndex = 20
pattern2.lastIndex = 0
pattern2.lastIndex = 6
pattern2.lastIndex = 100
正则表达式元字符是包含特殊含义的字符,
它们可以控制匹配模式的方式,
反斜杠后的元字符将失去其特殊意义。
元字符 | 匹配情况 |
---|---|
. | 匹配出换行符外的任意字符 |
代码:
/**
* 点元字符:匹配除换行符外的任意字符
*/
var pattern1 = /g..gle/;
var str1 = 'google';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test('g\\ngle') = " + pattern1.test('g\ngle'));
print("pattern1.test('g12gle') = " + pattern1.test('g12gle'));
print("pattern1.test('g3gle') = " + pattern1.test('g3gle'));
运行结果:
pattern1.test(str1) = true
pattern1.test('g\ngle') = false
pattern1.test('g12gle') = true
pattern1.test('g3gle') = false
元字符 | 匹配情况 |
---|---|
x* | 匹配0个或多个x |
x? | 匹配0个或1个x |
x+ | 匹配1个或多个x |
(xyz)+ | 匹配1个或多个(xyz) |
x{m,n} | 匹配m个到n个x |
其中,前面的x也可以与点元字符一起使用,
例如:/.?/表示0个或一个任意字符
代码:
/**
* 重复字符
*/
var pattern1 = /go*gle/;
var str1 = 'gooooooogle';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test('gobbbgle') = " + pattern1.test('gobbbgle'));
print("pattern1.test('go12gle') = " + /go+gle/.test('go12gle'));
print("pattern1.test('ggle') = " + pattern1.test('ggle'));
var pattern2 = /go+gle/;
print("pattern2.test(str1) = " + pattern2.test(str1));
print("pattern2.test('gogle') = " + pattern2.test('gogle'));
print("pattern2.test('ggle') = " + pattern2.test('ggle'));
var pattern3 = /go?gle/;
print("pattern3.test(str1) = " + pattern3.test(str1));
print("pattern3.test('gogle') = " + pattern3.test('gogle'));
print("pattern3.test('ggle') = " + pattern3.test('ggle'));
print("pattern3.test('gXgle') = " + pattern3.test('gXgle'));
var pattern4 = /g.?gle/;
print("pattern4.test('ggle') = " + pattern4.test('ggle'));
print("pattern4.test('gXgle') = " + pattern4.test('gXgle'));
print("pattern4.test('gXXgle') = " + pattern4.test('gXXgle'));
var pattern5 = /go{2,4}gle/;
var str2 = 'google';
print("pattern5.test(str2) = " + pattern5.test(str2));
print("pattern5.test('goooogle') = " + pattern5.test('goooogle'));
print("pattern5.test('gooooogle') = " + pattern5.test('gooooogle'));
print("pattern5.test('ggle') = " + pattern5.test('ggle'));
var pattern6 = /go{3}gle/;
print("pattern6.test('google') = " + pattern6.test('google'));
print("pattern6.test('gooogle') = " + pattern6.test('gooogle'));
print("pattern6.test('goooogle') = " + pattern6.test('goooogle'));
var pattern7 = /go{3,}gle/;
print("pattern7.test('google') = " + pattern7.test('google'));
print("pattern7.test('gooogle') = " + pattern7.test('gooogle'));
print("pattern7.test('gooooooogle') = " + pattern7.test('gooooooogle'));
运行结果:
pattern1.test(str1) = true
pattern1.test('gobbbgle') = false
pattern1.test('go12gle') = false
pattern1.test('ggle') = true
pattern2.test(str1) = true
pattern2.test('gogle') = true
pattern2.test('ggle') = false
pattern3.test(str1) = false
pattern3.test('gogle') = true
pattern3.test('ggle') = true
pattern3.test('gXgle') = false
pattern4.test('ggle') = true
pattern4.test('gXgle') = true
pattern4.test('gXXgle') = false
pattern5.test(str2) = true
pattern5.test('goooogle') = true
pattern5.test('gooooogle') = false
pattern5.test('ggle') = false
pattern6.test('google') = false
pattern6.test('gooogle') = true
pattern6.test('goooogle') = false
pattern7.test('google') = false
pattern7.test('gooogle') = true
pattern7.test('gooooooogle') = true
元字符 | 匹配情况 |
---|---|
. | 除换行符外的任意字符 |
[a-z0-9] | 括号中字符集中的任意字符 |
[^a-z0-9] | 不在括号中字符集中的任意字符 |
\d | 数字 |
\D | 非数字 |
\w | 字母数字下划线 |
\W | 非字母数字下划线 |
代码:
/**
* [a-z]表示26个小写字母的任意一个
*/
var pattern1 = /[a-z]oogle/;
var str1 = 'google';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test('boogle') = " + pattern1.test('boogle'));
print("pattern1.test('6oogle') = " + pattern1.test('6oogle'));
var pattern2 = /[A-Z]oogle/;
print("pattern2.test(str1) = " + pattern2.test(str1));
print("/[A-Z]oogle/i.test(str1) = " + /[A-Z]oogle/i.test(str1));
var pattern3 = /[0-9]oogle/;
print("pattern3.test(str1) = " + pattern3.test(str1));
print("pattern3.test('4oogle') = " + pattern3.test('4oogle'));
print("pattern3.test('44oogle') = " + pattern3.test('44oogle'));
var pattern4 = /[0-9]*oogle/; //[0-9]*表示0个或多个数字
print("pattern4.test('444oogle') = " + pattern4.test('444oogle'));
var pattern5 = /[a-zA-Z0-9]oogle/;
print("pattern5.test('boogle') = " + pattern5.test('boogle'));
print("pattern5.test('Boogle') = " + pattern5.test('Boogle'));
print("pattern5.test('6oogle') = " + pattern5.test('6oogle'));
print("pattern5.test('oogle') = " + pattern5.test('oogle'));
var pattern6 = /[^0-9]oogle/;
print("pattern6.test('444oogle') = " + pattern6.test('444oogle'));
print("pattern6.test('_oogle') = " + pattern6.test('_oogle'));
运行结果:
pattern1.test(str1) = true
pattern1.test('boogle') = true
pattern1.test('6oogle') = false
pattern2.test(str1) = false
/[A-Z]oogle/i.test(str1) = true
pattern3.test(str1) = false
pattern3.test('4oogle') = true
pattern3.test('44oogle') = true
pattern4.test('444oogle') = true
pattern5.test('boogle') = true
pattern5.test('Boogle') = true
pattern5.test('6oogle') = true
pattern5.test('oogle') = false
pattern6.test('444oogle') = false
pattern6.test('_oogle') = true
元字符 | 匹配情况 |
---|---|
^ | 行首匹配 |
$ | 行尾匹配 |
\A | 只有匹配字符串开始处 |
\b | 匹配单词边界,词在[]中时无效 |
\G | 匹配当前搜索的开始位置 |
\Z | 匹配字符串结束处或行尾 |
\z | 只匹配字符串结束处 |
代码:
/**
* 锚元字符
*/
var pattern1 = /^[0-9]oogle/;
var pattern2 = /^[0-9]+oogle/;
var str1 = '4oogle';
var str2 = '4444oogle';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test(str2) = " + pattern1.test(str2));
print("pattern2.test(str1) = " + pattern2.test(str1));
print("pattern2.test(str2) = " + pattern2.test(str2));
var pattern3 = /\woogle/;
var pattern4 = /\Woogle/;
print("pattern3.test('aoogle') = " + pattern3.test('aoogle'));
print("pattern3.test('_oogle') = " + pattern3.test('_oogle'));
print("pattern3.test('-oogle') = " + pattern3.test('-oogle'));
print("pattern4.test('aoogle') = " + pattern4.test('aoogle'));
print("pattern4.test('_oogle') = " + pattern4.test('_oogle'));
print("pattern4.test('-oogle') = " + pattern4.test('-oogle'));
var pattern5 = /\doogle/;
var pattern6 = /\Doogle/;
print("pattern5.test('aoogle') = " + pattern5.test('aoogle'));
print("pattern5.test('9oogle') = " + pattern5.test('9oogle'));
print("pattern6.test('aoogle') = " + pattern6.test('aoogle'));
print("pattern6.test('9oogle') = " + pattern6.test('9oogle'));
var pattern7 = /^google/;
var pattern8 = /^[a-z]oog[0-9]$/;
var str3 = 'This is google search engine';
print("pattern7.test(str3) = " + pattern7.test(str3));
print("pattern8.test(str3) = " + pattern8.test(str3));
print("pattern8.test('google') = " + pattern8.test('google'));
print("pattern8.test('goog5') = " + pattern8.test('goog5'));
运行结果:
pattern1.test(str1) = true
pattern1.test(str2) = false
pattern2.test(str1) = true
pattern2.test(str2) = true
pattern3.test('aoogle') = true
pattern3.test('_oogle') = true
pattern3.test('-oogle') = false
pattern4.test('aoogle') = false
pattern4.test('_oogle') = false
pattern4.test('-oogle') = true
pattern5.test('aoogle') = false
pattern5.test('9oogle') = true
pattern6.test('aoogle') = true
pattern6.test('9oogle') = false
pattern7.test(str3) = false
pattern8.test(str3) = false
pattern8.test('google') = false
pattern8.test('goog5') = true
元字符 | 匹配情况 |
---|---|
\0 | null |
\b | 空格 |
\f | 进纸 |
\n | 换行 |
\r | 回车 |
\t | 制表 |
\s | 空白空格制表换行 |
\S | 非空白字符 |
代码:
/**
* 空白字符
*/
var pattern1 = /goo gle/;
var str1 = 'goo gle';
print("pattern1.test(str1) = " + pattern1.test(str1));
var pattern2 = /goo\sgle/;
var str2 = 'goo gle';
print("pattern2.test(str2) = " + pattern2.test(str2));
print("/goo\\bgle/.test(str2) = " + /goo\bgle/.test(str2));
var pattern3 = /google\b/;
print("pattern3.test('google') = " + pattern3.test('google'));
print("pattern3.test('googleee') = " + pattern3.test('googleee'));
运行结果:
pattern1.test(str1) = true
pattern2.test(str2) = true
/goo\bgle/.test(str2) = false
pattern3.test('google') = true
pattern3.test('googleee') = false
被|隔开的字符串有一个匹配到就返回true
代码:
var pattern1 = /google|baidu|bing/;
var str1 = 'bing';
var str2 = 'soso';
var str3 = 'this is google';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test(str2) = " + pattern1.test(str2));
print("pattern1.test(str3) = " + pattern1.test(str3));
运行结果
pattern1.test(str1) = true
pattern1.test(str2) = false
pattern1.test(str3) = true
可以把分组后的字符串看成一个字符
元字符 | 匹配情况 |
---|---|
(string) | 用于反向引用的分组 |
\1或$1 | 匹配第一个分组中的内容 |
\2或$2 | 匹配第二个分组中的内容 |
\3或$3 | 匹配第三个分组中的内容 |
RegExp.$1表示获取模式中的一个分组对应的字符串,
在获取之前必须要运行一下正则表达式
代码:
/**
* 分组匹配
*/
var pattern1 = /google{4,8}/;
var pattern2 = /google{4,8}$/;
var str1 = 'googleeeeee';
print("pattern1.test(str1) = " + pattern1.test(str1));
print("pattern1.test('googleeeeeeeeeee') = " + pattern1.test('googleeeeeeeeeee'));
print("pattern2.test('googleee') = " + pattern2.test('googleee'));
print("pattern2.test('googleeee') = " + pattern2.test('googleeee'));
print("pattern2.test('googleeeeeeeeeee') = " + pattern2.test('googleeeeeeeeeee'));
var pattern3 = /(google){4,8}$/;
var str2 = 'googlee';
var str3 = 'googlegooglegooglegoogle';
print("pattern3.test(str2) = " + pattern3.test(str2));
print("pattern3.test(str3) = " + pattern3.test(str3));
/**
* 获取匹配到的字符串
*/
var pattern4 = /8(.*)8/;
var pattern5 = /8.*8/;
var str4 = 'this is 8google8';
print("pattern4.test(str4) = " + pattern4.test(str4));
print("RegExp.$1 = " + RegExp.$1);
print("pattern5.test(str4) = " + pattern5.test(str4));
print("RegExp.$1 = " + RegExp.$1);
var str5 = 'this is a 8GooGle8';
var ret1 = str5.replace(pattern4, '123');
print("ret1 = " + ret1);
var ret2 = str5.replace(pattern4, '<strong>GooGle</strong>');
print("ret2 = " + ret2);
var str6 = 'this is a 8BaiDu8';
var ret3 = str6.replace(pattern4, '<strong>$1</strong>');
print("ret3 = " + ret3);
var pattern6 = /(.*)\s(.*)/;
var str7 = 'google baidu';
var ret4 = str7.replace(pattern6, '$2 $1');
print("ret4 = " + ret4);
运行结果
pattern1.test(str1) = true
pattern1.test('googleeeeeeeeeee') = true
pattern2.test('googleee') = false
pattern2.test('googleeee') = true
pattern2.test('googleeeeeeeeeee') = false
pattern3.test(str2) = false
pattern3.test(str3) = true
pattern4.test(str4) = true
RegExp.$1 = google
pattern5.test(str4) = true
RegExp.$1 =
ret1 = this is a 123
ret2 = this is a <strong>GooGle</strong>
ret3 = this is a <strong>BaiDu</strong>
ret4 = baidu google
贪婪 | 惰性 |
---|---|
+ | +? |
? | ?? |
* | *? |
{n} | {n}? |
{n,} | {n,}? |
{n,m} | {n,m}? |
代码:
/**
* 贪婪与惰性
*/
var pattern1 = /[a-z]/;
var pattern2 = /[a-z]+/;
var pattern3 = /[a-z]+?/;
var pattern4 = /[a-z]+?/g;
var str1 = 'abcdef';
var ret1 = str1.replace(pattern1, '1');
var ret2 = str1.replace(pattern2, '1');
var ret3 = str1.replace(pattern3, '1');
var ret4 = str1.replace(pattern4, '1');
print("ret1 = " + ret1);
print("ret2 = " + ret2);
print("ret3 = " + ret3);
print("ret4 = " + ret4);
/**
* 禁止了贪婪,开启了全局
*/
var pattern5 = /8(.*)8/;
var pattern6 = /8(.*?)8/;
var pattern7 = /8(.*?)8/g;
var str2 = '8google8 8google8 8google8 8google8';
var ret5 = str2.replace(pattern5, '<strong>$1</strong>');
var ret6 = str2.replace(pattern6, '<strong>$1</strong>');
var ret7 = str2.replace(pattern7, '<strong>$1</strong>');
print("ret5 = " + ret5);
print("ret6 = " + ret6);
print("ret7 = " + ret7);
/**
* 除了正则表达式里两边的8不要匹配
*/
var pattern8 = /8([^8]*)8/g;
var pattern9 = /8(.*)8/g;
var str3 = '8google8 8google8 8google8';
var ret8 = str3.replace(pattern8, '<strong>$1</strong>');
var ret9 = str3.replace(pattern9, '<strong>$1</strong>');
print("ret8 = " + ret8);
print("ret9 = " + ret9);
运行结果:
ret1 = 1bcdef
ret2 = 1
ret3 = 1bcdef
ret4 = 111111
ret5 = <strong>google8 8google8 8google8 8google</strong>
ret6 = <strong>google</strong> 8google8 8google8 8google8
ret7 = <strong>google</strong> <strong>google</strong> <strong>google</strong> <strong>google</strong>
ret8 = <strong>google</strong> <strong>google</strong> <strong>google</strong>
ret9 = <strong>google8 8google8 8google</strong>
捕获性分组,所有的分组都捕获返回
非捕获性分组只需要在不需要捕获的分组前加上?:
代码:
/**
* 返回数组
*/
var pattern1 = /^[a-z]+\s[0-9]{4}$/;
var str1 = 'google 2015';
print("pattern1.test(str1) = " + pattern1.test(str1));
var ret1 = pattern1.exec(str1);
print("ret1 = " + ret1);
print("typeof ret1 = " + typeof ret1);
var pattern2 = /^[a-z]+/;
var ret2 = pattern2.exec(str1);
print("ret2 = " + ret2);
print("typeof ret2 = " + typeof ret2);
/**
* 分组返回
*/
var pattern3 = /^([a-z]+)\s([0-9]{4})$/;
var ret3 = pattern3.exec(str1);
print("ret3 = " + ret3);
print("typeof ret3 = " + typeof ret3);
print("ret3[0] = " + ret3[0]); //匹配到的整个字符串
print("ret3[1] = " + ret3[1]); //匹配到的第1个字符串
print("ret3[2] = " + ret3[2]); //匹配到的第2个字符串
/**
* 捕获性分组,所有的分组都捕获返回
* 非捕获性分组只需要在不需要捕获的分组前加上?:
*/
var pattern4 = /(\d+)([a-z]+)/;
var pattern5 = /(\d+)(?:[a-z]+)/;
var str2 = '123abc';
print("pattern4.exec(str2) = " + pattern4.exec(str2));
print("pattern5.exec(str2) = " + pattern5.exec(str2));
/**
* 分组嵌套
*/
var pattern6 = /(a?(b?(c?)))/;
var str3 = 'abc';
var ret4 = pattern6.exec(str3);
print("ret4 = " + ret4);
print("ret4[1] = " + ret4[1]); //匹配到第1个分组(a?(b?(c?)))
print("ret4[2] = " + ret4[2]); //匹配到第2个分组(b?(c?))
print("ret4[3] = " + ret4[3]); //匹配到第3个分组(c?)
运行结果:
pattern1.test(str1) = true
ret1 = google 2015
typeof ret1 = object
ret2 = google
typeof ret2 = object
ret3 = google 2015,google,2015
typeof ret3 = object
ret3[0] = google 2015
ret3[1] = google
ret3[2] = 2015
pattern4.exec(str2) = 123abc,123,abc
pattern5.exec(str2) = 123abc,123
ret4 = abc,abc,bc,c
ret4[1] = abc
ret4[2] = bc
ret4[3] = c
匹配的字符串后面必须是另一个匹配的字符串
代码:
/**
* 前瞻捕获
*/
var pattern1 = /goo(?=gle)/;
var str1 = 'goobbb';
var str2 = 'google';
var ret1 = pattern1.exec(str1);
var ret2 = pattern1.exec(str2);
print("ret1 = " + ret1);
print("ret2 = " + ret2);
运行结果:
ret1 = null
ret2 = goo
代码:
/**
* 特殊字符匹配
* 用反斜杠转义正则里的特殊字符才能匹配
*/
var pattern2 = /\[/;
var str3 = '[';
var ret3 = pattern2.exec(str3);
print("ret3 = " + ret3);
运行结果:
ret3 = [
代码:
/**
* 换行模式
*/
var pattern3 = /^\d+/;
var pattern4 = /^\d+/g;
var pattern5 = /^\d+/gm;
var str3 = '1.baidu\n2.google\n3.bing';
var ret3 = str3.replace(pattern3, '#');
var ret4 = str3.replace(pattern4, '#');
var ret5 = str3.replace(pattern5, '#');
print("ret3 = \n" + ret3);
print("ret4 = \n" + ret4);
print("ret5 = \n" + ret5);
运行结果:
ret3 =
#.baidu
2.google
3.bing
ret4 =
#.baidu
2.google
3.bing
ret5 =
#.baidu
#.google
#.bing
代码如下:
/**
* 邮政编码
* 必须是六位,必须是数字,首位不为零
*/
var pattern1 = /[1-9][0-9]{5}/;
var str1 = 'This is 224000!';
var ret1 = pattern1.test(str1);
print("ret1 = " + ret1);
/**
* 压缩文件
* (文件名).(扩展名)
*/
var pattern2 = /^\w+\.zip|gz|rar$/;
var str2 = '213.zip';
var ret2 = pattern2.test(str2);
print("ret2 = " + ret2);
var str3 = '21-3.zip';
var ret3 = pattern2.test(str3);
print("ret3 = " + ret3);
/**
* 删除空格
*/
var pattern3 = /\s/g;
var str4 = '111 222 33 44 555';
var ret4 = str4.replace(pattern3, '');
print("ret4 = " + ret4);
/**
* 删除首尾空格
*/
var pattern4 = /^\s+/;
var str5 = ' goo gle ';
var ret5 = str5.replace(pattern4, '');
print("ret5 = |" + ret5 + "|");
var pattern5 = /\s+$/;
var ret6 = ret5.replace(pattern5, '');
print("ret6 = |" + ret6 + "|");
/**
* 删除首尾空格
* 使用惰性模式的分组匹配
*/
var pattern6 = /^\s+(.+)\s+$/;
var str6 = ' goo gle ';
var ret7 = pattern6.exec(str6);
var str7 = ret7[1];
print("str7 = |" + str7 + "|");
var pattern7 = /^\s+(.+?)\s+$/;
var str7 = ' goo gle ';
var ret8 = pattern7.exec(str7);
var str8 = ret8[1];
print("str8 = |" + str8 + "|");
var ret9 = str7.replace(pattern7, '$1');
print("ret9 = |" + ret9 + "|");
/**
* 简单电子邮件匹配
*/
var pattern8 = /^([\w\.\-]+)@([\w\-]+)\.([a-zA-Z]{2,4})$/;
var str8 = 'yuki@163.com';
var ret10 = pattern8.test(str8);
print("ret10 = " + ret10);
运行结果:
ret1 = true
ret2 = true
ret3 = false
ret4 = 1112223344555
ret5 = |goo gle |
ret6 = |goo gle|
str7 = |goo gle |
str8 = |goo gle|
ret9 = |goo gle|
ret10 = true
运行上面的javascript代码使用的是Eclipse Kepler自带的Rhino解释器,
原来使用IntelliJ IDEA的童鞋可以试试用一下Eclipse Kepler。
本文的编辑使用了作业部落的css样式以及它们的markdown编辑器
点击可以访问原文:https://www.zybuluo.com/thorncorona/note/71406
如果觉得此文不错的话可以点击下面红色的小按钮关注我
更多文章请点击http://www.cnblogs.com/kodoyang/访问我的博客园主页。
大家阅读愉快。
参考链接:
- 李炎恢的正则表达式专题
- 参考w3school
孔东阳
二〇一五年二月十四日