javascript正则表达式

javascript正则表达式


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

 

Ⅰ.创建正则表达式

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

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

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

  

/**
* new运算符创建正则表达式
*/
var box1 =newRegExp('box');//第一个参数是模式字符串
print('box1 = '+ box1);
var box2 =newRegExp('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 =newRegExp('box');//验证test
var str1 ='box';
var str2 ='Box';
print('pattern.test(str1) = '+ pattern.test(str1));
print('pattern.test(str2) = '+ pattern.test(str2));
var pattern2 =newRegExp('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 = "+typeofnull);

运行结果如下:

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

 

Ⅵ.获取控制

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

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

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

代码:

/**
* 点元字符:匹配除换行符外的任意字符
*/
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
 

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


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


javascript正则表达式简介的更多相关文章

  1. JavaScript正则表达式简介(一)

    一.正则表达式 正则表达式Regular Expression,可以简写为regexp.regex或是RE. 正则表达式使用单个字符串来描述或是匹配一系列符合某个句法规则的字符串模型. 按照某种规则去 ...

  2. JavaScript正则表达式详解(一)正则表达式入门

    JavaScript正则表达式是很多JavaScript开发人员比较头疼的事情,也很多人不愿意学习,只是必要的时候上网查一下就可以啦~本文中详细的把JavaScript正则表达式的用法进行了列表,希望 ...

  3. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  4. javascript正则表达式语法

    1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...

  5. javascript 正则表达式 详细入门教程

    1.什么是正则表达式 定义: 一个用来搜索.匹配.处理一些符合特定语法规则的一个强大的字符串处理工具. 用途: 进行特定字符和字符串的搜索 替换字符串中指定的字符或字符串 验证字符串是否符合需求 2. ...

  6. JavaScript正则表达式,你真的知道?

    一.前言 粗浅的编写正则表达式,是造成性能瓶颈的主要原因.如下: var reg1 = /(A+A+)+B/; var reg2 = /AA+B/; 上述两个正则表达式,匹配效果是一样的,但是,效率就 ...

  7. 【JS】javascript 正则表达式 大全 总结

    javascript 正则表达式 大全 总结 参考整理了一些javascript正则表达式 目的一:自我复习归纳总结 目的二:共享方便大家搜索 微信:wixf150 验证数字:^[0-9]*$ 验证n ...

  8. 理清JavaScript正则表达式--上篇

    在JavaScript中,正则表达式由RegExp对象表示.RegExp对象呢,又可以通过直接量和构造函数RegExp两种方式创建,分别如下: //直接量 var re = /pattern/[g | ...

  9. 理清JavaScript正则表达式--下篇

    紧接:"理清JavaScript正则表达式--上篇". 正则在String类中的应用 类String支持四种利用正则表达式的方法.分别是search.replace.match和s ...

随机推荐

  1. 如何避免JSP页面自动生成session对象?为什么要这么做?

    JSP // 在默认情况下,在对一个JSP页面发出请求时,如果session还没有建立,JSP页面会自动为请求建立一个session对象,但是session是比较消耗资源的,如果没必要保持和使用ses ...

  2. struts2与struts1整合,java.lang.InstantiationException, Exception occurred during processing request: null

    做了2个action,其中一个运行没有问题,另一个报错,看下面的报错信息,再看了看struts.xml,因为没有给GetBooks这个action配置actionform,所以就导致报null.下面是 ...

  3. PowerDesigner中name和code取消自动关联

    PowerDesigner中,如果修改了某个字段的name,其code也跟着修改,如果想取消,可以如下操作 解决方法如下: 1.选择Tools->GeneralOptions...菜单,出现Ge ...

  4. USACO Section 3.2: Factorials

    这题注意要保存%10000的数. /* ID: yingzho1 LANG: C++ TASK: fact4 */ #include <iostream> #include <fst ...

  5. c# 浏览器区别

    c#   浏览器区别 思路:浏览器本身独有的属性来区别: 1.window对象的属性来区别: window.attachEvent               IEwindow.addEventLis ...

  6. zabbix接口调用注意事项--Python

    不知道该怎么写,但是明显得写点什么,担心时间长了,忘记,再回顾时又要重新摸索一遍 一.Request:post params: 1. 第一层的参数处理: 第一层的参数设置为变量 2. 其他层参数格式不 ...

  7. INDIGO STUDIO神器!快速创建WEB、移动应用的交互原型工具【转】

    转自:http://www.uisdc.com/indigo-studio-wireframe-interactive-uis 这套最新的设计工具出自Indigo工作室,永久免费,有mac版本和WIN ...

  8. 使用Gson解析复杂的json数据

    Gson解析复杂的json数据 最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的jso ...

  9. hibernate的save()和persit()之间的区别

    这个问题啊,我在传智的Hibernate 视频上有小段讲解,save() 和persist() 都是持久化的保存,这两个方法在已经开启事物的情况下没多大区别:在不开启事物的时候save()方法会把数据 ...

  10. 【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

    原文在: https://yq.aliyun.com/articles/40353 这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html 先来一段S ...