首页 > 网站 > WEB开发 > 正文

javascript正则表达式简介

2024-04-27 14:12:50
字体:
来源:转载
供稿:网友

javascript正则表达式简介


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/gimbox3 = /Box/box4 = /Box/gi

Ⅱ.测试正则表达式

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

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

代码如下:

/*** new创建的正则表达式的test*/var pattern =newRegExp('box');//验证testvar 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) = truepattern.test(str2) = falsepattern2.test(str1) = truepattern2.test(str2) = truepattern3.test(str3) = true/box/i.test('This is a Box') = trueret1 = Boxtypeof ret1 = objectret2 = nulltypeof ret2 = objecttypeof 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) = boxret1 = box,boxret1[0] = boxret2 = 10'This is a BOX!'.search(pattern3) = -1ret3 = 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 = 3ret6 = 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 = googleRegExp.lastParen = GRegExp.multiline = falseRegExp['$&'] = googleRegExp.$_ = 

Ⅴ.正则表达式实例属性

属性含义
global全局是否设置