[关闭]
@thorncorona 2015-02-27T21:44:21.000000Z 字数 19000 阅读 1959

javascript正则表达式

正则表达式


regular expression是一个描述字符模式的对象;
ECMAScript中的RegExp类表示正则表达式;
String和RegExp都定义了使用正则表达式进行强大的模式匹配、文本检索和替换的函数;
正则表达式主要用来验证客户端的输入数据;

Ⅰ.创建正则表达式

类似于创建字符串,可以使用new运算符也可以采用字面值;

参数 含义
g 全局匹配
i 忽略大小写
m 多行匹配

下文中的javascript代码是通过 EclipseKepler 的 rhino 解释的
代码如下:

  1. /**
  2. * new运算符创建正则表达式
  3. */
  4. var box1 = new RegExp('box'); //第一个参数是模式字符串
  5. print('box1 = ' + box1);
  6. var box2 = new RegExp('box', 'gim'); //第二个参数是模式修饰符
  7. print('box2 = ' + box2);
  8. /**
  9. * 字面值创建正则表达式
  10. */
  11. var box3 = /Box/; //字面值方式的正则表达式
  12. print('box3 = ' + box3);
  13. var box4 = /Box/ig; //在第二个斜杠后加上模式修饰符
  14. print('box4 = ' + box4);

运行结果如下:

box1 = /box/
box2 = /box/gim
box3 = /Box/
box4 = /Box/gi

Ⅱ.测试正则表达式

RegExp对象包含两个方法,test()和exec()

方法 功能
test 在字符串中测试模式匹配,返回true或false
exec 在字符串中执行匹配搜索,返回结果数组

代码如下:

  1. /**
  2. * new创建的正则表达式的test
  3. */
  4. var pattern = new RegExp('box'); //验证test
  5. var str1 = 'box';
  6. var str2 = 'Box';
  7. print('pattern.test(str1) = ' + pattern.test(str1));
  8. print('pattern.test(str2) = ' + pattern.test(str2));
  9. var pattern2 = new RegExp('box', 'i'); //不区分大小写
  10. print('pattern2.test(str1) = ' + pattern2.test(str1));
  11. print('pattern2.test(str2) = ' + pattern2.test(str2));
  12. /**
  13. * 使用一条语句实现的正则匹配
  14. */
  15. var pattern3 = /box/i;
  16. var str3 = 'This is a Box';
  17. print("pattern3.test(str3) = " + pattern3.test(str3));
  18. print("/box/i.test('This is a Box') = " + /box/i.test('This is a Box'));
  19. /**
  20. * exec方法
  21. * 返回匹配到的字符串数组,如果没有就返回null
  22. */
  23. var pattern4 = /box/i;
  24. var ret1 = pattern4.exec(str3);
  25. print("ret1 = " + ret1);
  26. print("typeof ret1 = " + typeof ret1);
  27. var str4 = 'ssss';
  28. var ret2 = pattern4.exec(str4);
  29. print("ret2 = " + ret2);
  30. print("typeof ret2 = " + typeof ret2);
  31. 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拆分的数组

代码如下:

  1. /**
  2. * match方法
  3. * 如果不开启全局就只返回匹配到的第一个字符串
  4. * 如果开启全局就返回所有的被匹配字符串数组
  5. */
  6. var pattern1 = /box/;
  7. var pattern2 = /box/g; //开启全局
  8. var str = 'This is a box, That is a box!';
  9. print("str.match(pattern1) = " + str.match(pattern1));
  10. var ret1 = str.match(pattern2);
  11. print("ret1 = " + ret1);
  12. print("ret1[0] = " + ret1[0]);
  13. /**
  14. * search方法
  15. * 返回第一个匹配的位置,这里没有必要设置全局
  16. * 找不到返回-1
  17. */
  18. var pattern3 = /box/;
  19. var ret2 = str.search(pattern3);
  20. print("ret2 = " + ret2);
  21. print("'This is a BOX!'.search(pattern3) = " + 'This is a BOX!'.search(pattern3));
  22. /**
  23. * replace方法
  24. * 需要设置全局才会替换所有匹配到的字符串
  25. */
  26. var str2 = 'This is a box, That is a Box!';
  27. var pattern4 = /box/i;
  28. var pattern5 = /box/ig;
  29. var replacement = 'tom';
  30. var ret3 = str.replace(pattern4, replacement);
  31. var ret4 = str.replace(pattern5, replacement);
  32. print("ret3 = " + ret3);
  33. print("ret4 = " + ret4);
  34. /**
  35. * split方法
  36. */
  37. var str3 = 'This is a box! That is a Box!';
  38. var pattern6 = /!/g;
  39. var pattern7 = / /g;
  40. var ret5 = str3.split(pattern6);
  41. var ret6 = str3.split(pattern7);
  42. print("ret5 = " + ret5);
  43. print("ret5.length = " + ret5.length);
  44. print("ret6 = " + ret6);
  45. 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 $* 用于指定是否所有的表达式都用于多行的布尔值

代码如下:

  1. /**
  2. * 正则表达式静态属性
  3. */
  4. print("RegExp.input = " + RegExp.input);
  5. var pattern = /google/i;
  6. var str = 'This is a google! That is a GOOGLE!';
  7. pattern.test(str); //必须执行一下,静态属性才有效
  8. //需要在浏览器中使用alert运行才能打印出当前的匹配字符串
  9. print("RegExp.input = " + RegExp.input);
  10. print("RegExp.leftContext = " + RegExp.leftContext);
  11. print("RegExp.rightContext = " + RegExp.rightContext);
  12. print("RegExp.lastMatch = " + RegExp.lastMatch);
  13. var pattern2 = /(g)oogle/i;
  14. var str2 = 'This is a gogle! That is a GOOGLE!';
  15. pattern2.test(str2);
  16. print("RegExp.lastParen = " + RegExp.lastParen); //会打印匹配的在圆括号里字符串
  17. print("RegExp.multiline = " + RegExp.multiline);
  18. /**
  19. * 所有的属性可以通过短名来操作
  20. */
  21. var pattern3 = /google/i;
  22. var str3 = 'This is a google! That is a GOOGLE!';
  23. pattern3.test(str3);
  24. print("RegExp['$&'] = " + RegExp['$&']);
  25. 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 下次匹配将从字符串的哪里开始

代码如下:

  1. /**
  2. * 正则表达式实例属性
  3. */
  4. var pattern1 = /google/;
  5. var pattern2 = /google/g;
  6. var pattern3 = /google/ig;
  7. var pattern4 = /google/igm;
  8. print("pattern1.global = " + pattern1.global);
  9. print("pattern2.global = " + pattern2.global);
  10. print("pattern2.ignoreCase = " + pattern2.ignoreCase);
  11. print("pattern3.ignoreCase = " + pattern3.ignoreCase);
  12. print("pattern3.ignoreCase = " + pattern3.multiline);
  13. print("pattern4.ignoreCase = " + pattern4.multiline);
  14. print("pattern4.source = " + pattern4.source);
  15. /**
  16. * lastIndex动态属性
  17. */
  18. var str = 'google google google';
  19. print("pattern2.lastIndex = " + pattern2.lastIndex);
  20. for(var i = 6; --i != 0; ){
  21. pattern2.test(str);
  22. print("pattern2.lastIndex = " + pattern2.lastIndex);
  23. }
  24. pattern2.lastIndex = 100;
  25. 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

Ⅵ.获取控制

正则表达式元字符是包含特殊含义的字符,
它们可以控制匹配模式的方式,
反斜杠后的元字符将失去其特殊意义。

元字符 匹配情况
. 匹配出换行符外的任意字符

1.点元字符,匹配除换行符外的任意字符

代码:

  1. /**
  2. * 点元字符:匹配除换行符外的任意字符
  3. */
  4. var pattern1 = /g..gle/;
  5. var str1 = 'google';
  6. print("pattern1.test(str1) = " + pattern1.test(str1));
  7. print("pattern1.test('g\\ngle') = " + pattern1.test('g\ngle'));
  8. print("pattern1.test('g12gle') = " + pattern1.test('g12gle'));
  9. print("pattern1.test('g3gle') = " + pattern1.test('g3gle'));

运行结果:

pattern1.test(str1) = true
pattern1.test('g\ngle') = false
pattern1.test('g12gle') = true
pattern1.test('g3gle') = false

2.重复字符

元字符 匹配情况
x* 匹配0个或多个x
x? 匹配0个或1个x
x+ 匹配1个或多个x
(xyz)+ 匹配1个或多个(xyz)
x{m,n} 匹配m个到n个x

其中,前面的x也可以与点元字符一起使用,
例如:/.?/表示0个或一个任意字符

代码:

  1. /**
  2. * 重复字符
  3. */
  4. var pattern1 = /go*gle/;
  5. var str1 = 'gooooooogle';
  6. print("pattern1.test(str1) = " + pattern1.test(str1));
  7. print("pattern1.test('gobbbgle') = " + pattern1.test('gobbbgle'));
  8. print("pattern1.test('go12gle') = " + /go+gle/.test('go12gle'));
  9. print("pattern1.test('ggle') = " + pattern1.test('ggle'));
  10. var pattern2 = /go+gle/;
  11. print("pattern2.test(str1) = " + pattern2.test(str1));
  12. print("pattern2.test('gogle') = " + pattern2.test('gogle'));
  13. print("pattern2.test('ggle') = " + pattern2.test('ggle'));
  14. var pattern3 = /go?gle/;
  15. print("pattern3.test(str1) = " + pattern3.test(str1));
  16. print("pattern3.test('gogle') = " + pattern3.test('gogle'));
  17. print("pattern3.test('ggle') = " + pattern3.test('ggle'));
  18. print("pattern3.test('gXgle') = " + pattern3.test('gXgle'));
  19. var pattern4 = /g.?gle/;
  20. print("pattern4.test('ggle') = " + pattern4.test('ggle'));
  21. print("pattern4.test('gXgle') = " + pattern4.test('gXgle'));
  22. print("pattern4.test('gXXgle') = " + pattern4.test('gXXgle'));
  23. var pattern5 = /go{2,4}gle/;
  24. var str2 = 'google';
  25. print("pattern5.test(str2) = " + pattern5.test(str2));
  26. print("pattern5.test('goooogle') = " + pattern5.test('goooogle'));
  27. print("pattern5.test('gooooogle') = " + pattern5.test('gooooogle'));
  28. print("pattern5.test('ggle') = " + pattern5.test('ggle'));
  29. var pattern6 = /go{3}gle/;
  30. print("pattern6.test('google') = " + pattern6.test('google'));
  31. print("pattern6.test('gooogle') = " + pattern6.test('gooogle'));
  32. print("pattern6.test('goooogle') = " + pattern6.test('goooogle'));
  33. var pattern7 = /go{3,}gle/;
  34. print("pattern7.test('google') = " + pattern7.test('google'));
  35. print("pattern7.test('gooogle') = " + pattern7.test('gooogle'));
  36. 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

3.字符类匹配

元字符 匹配情况
. 除换行符外的任意字符
[a-z0-9] 括号中字符集中的任意字符
[^a-z0-9] 不在括号中字符集中的任意字符
\d 数字
\D 非数字
\w 字母数字下划线
\W 非字母数字下划线

代码:

  1. /**
  2. * [a-z]表示26个小写字母的任意一个
  3. */
  4. var pattern1 = /[a-z]oogle/;
  5. var str1 = 'google';
  6. print("pattern1.test(str1) = " + pattern1.test(str1));
  7. print("pattern1.test('boogle') = " + pattern1.test('boogle'));
  8. print("pattern1.test('6oogle') = " + pattern1.test('6oogle'));
  9. var pattern2 = /[A-Z]oogle/;
  10. print("pattern2.test(str1) = " + pattern2.test(str1));
  11. print("/[A-Z]oogle/i.test(str1) = " + /[A-Z]oogle/i.test(str1));
  12. var pattern3 = /[0-9]oogle/;
  13. print("pattern3.test(str1) = " + pattern3.test(str1));
  14. print("pattern3.test('4oogle') = " + pattern3.test('4oogle'));
  15. print("pattern3.test('44oogle') = " + pattern3.test('44oogle'));
  16. var pattern4 = /[0-9]*oogle/; //[0-9]*表示0个或多个数字
  17. print("pattern4.test('444oogle') = " + pattern4.test('444oogle'));
  18. var pattern5 = /[a-zA-Z0-9]oogle/;
  19. print("pattern5.test('boogle') = " + pattern5.test('boogle'));
  20. print("pattern5.test('Boogle') = " + pattern5.test('Boogle'));
  21. print("pattern5.test('6oogle') = " + pattern5.test('6oogle'));
  22. print("pattern5.test('oogle') = " + pattern5.test('oogle'));
  23. var pattern6 = /[^0-9]oogle/;
  24. print("pattern6.test('444oogle') = " + pattern6.test('444oogle'));
  25. 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

4.锚元字符

元字符 匹配情况
^ 行首匹配
$ 行尾匹配
\A 只有匹配字符串开始处
\b 匹配单词边界,词在[]中时无效
\G 匹配当前搜索的开始位置
\Z 匹配字符串结束处或行尾
\z 只匹配字符串结束处

代码:

  1. /**
  2. * 锚元字符
  3. */
  4. var pattern1 = /^[0-9]oogle/;
  5. var pattern2 = /^[0-9]+oogle/;
  6. var str1 = '4oogle';
  7. var str2 = '4444oogle';
  8. print("pattern1.test(str1) = " + pattern1.test(str1));
  9. print("pattern1.test(str2) = " + pattern1.test(str2));
  10. print("pattern2.test(str1) = " + pattern2.test(str1));
  11. print("pattern2.test(str2) = " + pattern2.test(str2));
  12. var pattern3 = /\woogle/;
  13. var pattern4 = /\Woogle/;
  14. print("pattern3.test('aoogle') = " + pattern3.test('aoogle'));
  15. print("pattern3.test('_oogle') = " + pattern3.test('_oogle'));
  16. print("pattern3.test('-oogle') = " + pattern3.test('-oogle'));
  17. print("pattern4.test('aoogle') = " + pattern4.test('aoogle'));
  18. print("pattern4.test('_oogle') = " + pattern4.test('_oogle'));
  19. print("pattern4.test('-oogle') = " + pattern4.test('-oogle'));
  20. var pattern5 = /\doogle/;
  21. var pattern6 = /\Doogle/;
  22. print("pattern5.test('aoogle') = " + pattern5.test('aoogle'));
  23. print("pattern5.test('9oogle') = " + pattern5.test('9oogle'));
  24. print("pattern6.test('aoogle') = " + pattern6.test('aoogle'));
  25. print("pattern6.test('9oogle') = " + pattern6.test('9oogle'));
  26. var pattern7 = /^google/;
  27. var pattern8 = /^[a-z]oog[0-9]$/;
  28. var str3 = 'This is google search engine';
  29. print("pattern7.test(str3) = " + pattern7.test(str3));
  30. print("pattern8.test(str3) = " + pattern8.test(str3));
  31. print("pattern8.test('google') = " + pattern8.test('google'));
  32. 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

5.空白字符

元字符 匹配情况
\0 null
\b 空格
\f 进纸
\n 换行
\r 回车
\t 制表
\s 空白空格制表换行
\S 非空白字符

代码:

  1. /**
  2. * 空白字符
  3. */
  4. var pattern1 = /goo gle/;
  5. var str1 = 'goo gle';
  6. print("pattern1.test(str1) = " + pattern1.test(str1));
  7. var pattern2 = /goo\sgle/;
  8. var str2 = 'goo gle';
  9. print("pattern2.test(str2) = " + pattern2.test(str2));
  10. print("/goo\\bgle/.test(str2) = " + /goo\bgle/.test(str2));
  11. var pattern3 = /google\b/;
  12. print("pattern3.test('google') = " + pattern3.test('google'));
  13. 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

6.或匹配

被|隔开的字符串有一个匹配到就返回true

代码:

  1. var pattern1 = /google|baidu|bing/;
  2. var str1 = 'bing';
  3. var str2 = 'soso';
  4. var str3 = 'this is google';
  5. print("pattern1.test(str1) = " + pattern1.test(str1));
  6. print("pattern1.test(str2) = " + pattern1.test(str2));
  7. print("pattern1.test(str3) = " + pattern1.test(str3));

运行结果

pattern1.test(str1) = true
pattern1.test(str2) = false
pattern1.test(str3) = true

7.分组匹配

可以把分组后的字符串看成一个字符

元字符 匹配情况
(string) 用于反向引用的分组
\1或$1 匹配第一个分组中的内容
\2或$2 匹配第二个分组中的内容
\3或$3 匹配第三个分组中的内容

RegExp.$1表示获取模式中的一个分组对应的字符串,
在获取之前必须要运行一下正则表达式

代码:

  1. /**
  2. * 分组匹配
  3. */
  4. var pattern1 = /google{4,8}/;
  5. var pattern2 = /google{4,8}$/;
  6. var str1 = 'googleeeeee';
  7. print("pattern1.test(str1) = " + pattern1.test(str1));
  8. print("pattern1.test('googleeeeeeeeeee') = " + pattern1.test('googleeeeeeeeeee'));
  9. print("pattern2.test('googleee') = " + pattern2.test('googleee'));
  10. print("pattern2.test('googleeee') = " + pattern2.test('googleeee'));
  11. print("pattern2.test('googleeeeeeeeeee') = " + pattern2.test('googleeeeeeeeeee'));
  12. var pattern3 = /(google){4,8}$/;
  13. var str2 = 'googlee';
  14. var str3 = 'googlegooglegooglegoogle';
  15. print("pattern3.test(str2) = " + pattern3.test(str2));
  16. print("pattern3.test(str3) = " + pattern3.test(str3));
  17. /**
  18. * 获取匹配到的字符串
  19. */
  20. var pattern4 = /8(.*)8/;
  21. var pattern5 = /8.*8/;
  22. var str4 = 'this is 8google8';
  23. print("pattern4.test(str4) = " + pattern4.test(str4));
  24. print("RegExp.$1 = " + RegExp.$1);
  25. print("pattern5.test(str4) = " + pattern5.test(str4));
  26. print("RegExp.$1 = " + RegExp.$1);
  27. var str5 = 'this is a 8GooGle8';
  28. var ret1 = str5.replace(pattern4, '123');
  29. print("ret1 = " + ret1);
  30. var ret2 = str5.replace(pattern4, '<strong>GooGle</strong>');
  31. print("ret2 = " + ret2);
  32. var str6 = 'this is a 8BaiDu8';
  33. var ret3 = str6.replace(pattern4, '<strong>$1</strong>');
  34. print("ret3 = " + ret3);
  35. var pattern6 = /(.*)\s(.*)/;
  36. var str7 = 'google baidu';
  37. var ret4 = str7.replace(pattern6, '$2 $1');
  38. 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

8.贪婪与惰性

贪婪 惰性
+ +?
? ??
* *?
{n} {n}?
{n,} {n,}?
{n,m} {n,m}?

代码:

  1. /**
  2. * 贪婪与惰性
  3. */
  4. var pattern1 = /[a-z]/;
  5. var pattern2 = /[a-z]+/;
  6. var pattern3 = /[a-z]+?/;
  7. var pattern4 = /[a-z]+?/g;
  8. var str1 = 'abcdef';
  9. var ret1 = str1.replace(pattern1, '1');
  10. var ret2 = str1.replace(pattern2, '1');
  11. var ret3 = str1.replace(pattern3, '1');
  12. var ret4 = str1.replace(pattern4, '1');
  13. print("ret1 = " + ret1);
  14. print("ret2 = " + ret2);
  15. print("ret3 = " + ret3);
  16. print("ret4 = " + ret4);
  17. /**
  18. * 禁止了贪婪,开启了全局
  19. */
  20. var pattern5 = /8(.*)8/;
  21. var pattern6 = /8(.*?)8/;
  22. var pattern7 = /8(.*?)8/g;
  23. var str2 = '8google8 8google8 8google8 8google8';
  24. var ret5 = str2.replace(pattern5, '<strong>$1</strong>');
  25. var ret6 = str2.replace(pattern6, '<strong>$1</strong>');
  26. var ret7 = str2.replace(pattern7, '<strong>$1</strong>');
  27. print("ret5 = " + ret5);
  28. print("ret6 = " + ret6);
  29. print("ret7 = " + ret7);
  30. /**
  31. * 除了正则表达式里两边的8不要匹配
  32. */
  33. var pattern8 = /8([^8]*)8/g;
  34. var pattern9 = /8(.*)8/g;
  35. var str3 = '8google8 8google8 8google8';
  36. var ret8 = str3.replace(pattern8, '<strong>$1</strong>');
  37. var ret9 = str3.replace(pattern9, '<strong>$1</strong>');
  38. print("ret8 = " + ret8);
  39. 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>

9.返回数组

捕获性分组,所有的分组都捕获返回
非捕获性分组只需要在不需要捕获的分组前加上?:

代码:

  1. /**
  2. * 返回数组
  3. */
  4. var pattern1 = /^[a-z]+\s[0-9]{4}$/;
  5. var str1 = 'google 2015';
  6. print("pattern1.test(str1) = " + pattern1.test(str1));
  7. var ret1 = pattern1.exec(str1);
  8. print("ret1 = " + ret1);
  9. print("typeof ret1 = " + typeof ret1);
  10. var pattern2 = /^[a-z]+/;
  11. var ret2 = pattern2.exec(str1);
  12. print("ret2 = " + ret2);
  13. print("typeof ret2 = " + typeof ret2);
  14. /**
  15. * 分组返回
  16. */
  17. var pattern3 = /^([a-z]+)\s([0-9]{4})$/;
  18. var ret3 = pattern3.exec(str1);
  19. print("ret3 = " + ret3);
  20. print("typeof ret3 = " + typeof ret3);
  21. print("ret3[0] = " + ret3[0]); //匹配到的整个字符串
  22. print("ret3[1] = " + ret3[1]); //匹配到的第1个字符串
  23. print("ret3[2] = " + ret3[2]); //匹配到的第2个字符串
  24. /**
  25. * 捕获性分组,所有的分组都捕获返回
  26. * 非捕获性分组只需要在不需要捕获的分组前加上?:
  27. */
  28. var pattern4 = /(\d+)([a-z]+)/;
  29. var pattern5 = /(\d+)(?:[a-z]+)/;
  30. var str2 = '123abc';
  31. print("pattern4.exec(str2) = " + pattern4.exec(str2));
  32. print("pattern5.exec(str2) = " + pattern5.exec(str2));
  33. /**
  34. * 分组嵌套
  35. */
  36. var pattern6 = /(a?(b?(c?)))/;
  37. var str3 = 'abc';
  38. var ret4 = pattern6.exec(str3);
  39. print("ret4 = " + ret4);
  40. print("ret4[1] = " + ret4[1]); //匹配到第1个分组(a?(b?(c?)))
  41. print("ret4[2] = " + ret4[2]); //匹配到第2个分组(b?(c?))
  42. 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

10.前瞻捕获

匹配的字符串后面必须是另一个匹配的字符串

代码:

  1. /**
  2. * 前瞻捕获
  3. */
  4. var pattern1 = /goo(?=gle)/;
  5. var str1 = 'goobbb';
  6. var str2 = 'google';
  7. var ret1 = pattern1.exec(str1);
  8. var ret2 = pattern1.exec(str2);
  9. print("ret1 = " + ret1);
  10. print("ret2 = " + ret2);

运行结果:

ret1 = null
ret2 = goo

11.特殊字符匹配

代码:

  1. /**
  2. * 特殊字符匹配
  3. * 用反斜杠转义正则里的特殊字符才能匹配
  4. */
  5. var pattern2 = /\[/;
  6. var str3 = '[';
  7. var ret3 = pattern2.exec(str3);
  8. print("ret3 = " + ret3);

运行结果:

ret3 = [

12.换行匹配

代码:

  1. /**
  2. * 换行模式
  3. */
  4. var pattern3 = /^\d+/;
  5. var pattern4 = /^\d+/g;
  6. var pattern5 = /^\d+/gm;
  7. var str3 = '1.baidu\n2.google\n3.bing';
  8. var ret3 = str3.replace(pattern3, '#');
  9. var ret4 = str3.replace(pattern4, '#');
  10. var ret5 = str3.replace(pattern5, '#');
  11. print("ret3 = \n" + ret3);
  12. print("ret4 = \n" + ret4);
  13. print("ret5 = \n" + ret5);

运行结果:

ret3 = 
#.baidu
2.google
3.bing
ret4 = 
#.baidu
2.google
3.bing
ret5 = 
#.baidu
#.google
#.bing

Ⅶ.常用正则表达式

代码如下:

  1. /**
  2. * 邮政编码
  3. * 必须是六位,必须是数字,首位不为零
  4. */
  5. var pattern1 = /[1-9][0-9]{5}/;
  6. var str1 = 'This is 224000!';
  7. var ret1 = pattern1.test(str1);
  8. print("ret1 = " + ret1);
  9. /**
  10. * 压缩文件
  11. * (文件名).(扩展名)
  12. */
  13. var pattern2 = /^\w+\.zip|gz|rar$/;
  14. var str2 = '213.zip';
  15. var ret2 = pattern2.test(str2);
  16. print("ret2 = " + ret2);
  17. var str3 = '21-3.zip';
  18. var ret3 = pattern2.test(str3);
  19. print("ret3 = " + ret3);
  20. /**
  21. * 删除空格
  22. */
  23. var pattern3 = /\s/g;
  24. var str4 = '111 222 33 44 555';
  25. var ret4 = str4.replace(pattern3, '');
  26. print("ret4 = " + ret4);
  27. /**
  28. * 删除首尾空格
  29. */
  30. var pattern4 = /^\s+/;
  31. var str5 = ' goo gle ';
  32. var ret5 = str5.replace(pattern4, '');
  33. print("ret5 = |" + ret5 + "|");
  34. var pattern5 = /\s+$/;
  35. var ret6 = ret5.replace(pattern5, '');
  36. print("ret6 = |" + ret6 + "|");
  37. /**
  38. * 删除首尾空格
  39. * 使用惰性模式的分组匹配
  40. */
  41. var pattern6 = /^\s+(.+)\s+$/;
  42. var str6 = ' goo gle ';
  43. var ret7 = pattern6.exec(str6);
  44. var str7 = ret7[1];
  45. print("str7 = |" + str7 + "|");
  46. var pattern7 = /^\s+(.+?)\s+$/;
  47. var str7 = ' goo gle ';
  48. var ret8 = pattern7.exec(str7);
  49. var str8 = ret8[1];
  50. print("str8 = |" + str8 + "|");
  51. var ret9 = str7.replace(pattern7, '$1');
  52. print("ret9 = |" + ret9 + "|");
  53. /**
  54. * 简单电子邮件匹配
  55. */
  56. var pattern8 = /^([\w\.\-]+)@([\w\-]+)\.([a-zA-Z]{2,4})$/;
  57. var str8 = 'yuki@163.com';
  58. var ret10 = pattern8.test(str8);
  59. 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

Ⅷ.Another

运行上面的javascript代码使用的是Eclipse Kepler自带的Rhino解释器,
原来使用IntelliJ IDEA的童鞋可以试试用一下Eclipse Kepler。

本文的编辑使用了作业部落的css样式以及它们的markdown编辑器
点击可以访问原文:https://www.zybuluo.com/thorncorona/note/71406

如果觉得此文不错的话可以点击下面红色的小按钮关注我
更多文章请点击http://www.cnblogs.com/kodoyang/访问我的博客园主页。
大家阅读愉快。



参考链接:
- 李炎恢的正则表达式专题
- 参考w3school


孔东阳
二〇一五年二月十四日


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注