说说正则表达式
正则表达式
var a=/\d/g;//a 的属性global: trueignoreCase: falselastIndex: 0multiline: falsesource: "\d"
关于正则表达式的一些表示方法这里不做说明....
正则表达式的方法
compile方法
....实在想不通为啥要这个方法
test方法
检测给定的字符串是否符合该正则表达式规则。
test的用法
参数为字符串,如果匹配,返回true,否则返回false。
var reg2=/\d/g;var r=reg2.test('2349efjo');r//trueRegExp.lastMatch//"2"RegExp.lastParen//""RegExp.input//"2349efjo"RegExp.leftContext//""RegExp.rightContext//"349efjo"RegExp.mutiline//false
结论
- 全局的RegExp对象在匹配后会带上一些属性。
- input: 对字符串参数的引用。
- lastMatch: 最近一次的匹配项
- leftContext: lastMatch左边的内容
- rightContext: lastMatch右边的内容
- lastParen: 最近一次匹配的捕获组,没有捕获组则为空
- multiline: 是否所有表达式都使用了多行模式,如果是则为true, 否则为false。
1− 9: 用于匹配对应的捕获组。
exec方法
执行匹配,返回一个匹配的数组,如果没有匹配上则返回null。
exec用法
var r=/(\w+)(\d+)-/g;r.exec('efefj2193902-84')//["efefj2193902-", "efefj219390", "2"]r.lastIndex//13r.exec('ejfiefjjeo213487329487294235897498327948-')//["487329487294235897498327948-", "48732948729423589749832794", "8"]r.lastIndex//41r.lastIndex=0r.exec('ejfiefjjeo213487329487294235897498327948-')//["ejfiefjjeo213487329487294235897498327948-", "ejfiefjjeo21348732948729423589749832794", "8"]RegExp.lastMatch//"ejfiefjjeo213487329487294235897498327948-"RegExp.leftContext//""RegExp.rightContext//""RegExp.lastParen//"8"RegExp.multiline//falseRegExp.input//"ejfiefjjeo213487329487294235897498327948-"
结论
- 返回的数组的项依次为[匹配整个正则的字符串,匹配第一个子表达式的字符串,匹配第二个子表达式的字符串......]
- 在执行exec的过程中, 正则表达式的lastIndex方法记录了匹配到的字符串的最后一个索引后面的位置(即索引+1).
- lastIndex是可以重新被赋值的,如果后面还有字符串要匹配,要把lastIndex重置为0。
- 执行exec对全局RegExp对象的影响,属性在test时已经介绍过。
和正则表达式相关的字符串方法
match方法
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
match方法的使用
其参数是一个字符串或正则表达式.
找出匹配指定参数的字符串并组成数组返回
数组有两个属性,分别是index和input属性
index属性指定了匹配字符的起始字符在原字符串中的索引,input是对原字符串的引用。
var ex=/\d*/g;'1221343'.match(ex)//["1221343", ""]
var ex=/do/g;var str="doesesdoes";str.match(ex);//['do','do'];str.match(ex).index//undefinedstr.match(ex).input//undefined
var str='helloworld';srt.match('o');//['o']str.match('o').index;//4str.match('o').input;//helloworld
var str='helloworld';str.match(/o/);//["o"]str.match(/o/).index//4str.match(/o/).input//"helloworld"
'efhu'.match('1')//null'efhu'.match('1').index//VM1036:2 Uncaught TypeError: Cannot read property 'index' of null'efhu'.match('1').input//VM1041:2 Uncaught TypeError: Cannot read property 'input' of null
结论:
- 返回数组是任意匹配到指定规则的字符串,当然也包括原字符串本身。
- 全局匹配时,会找出所有符合条件的字符串,而非全局匹配只能得到最早的匹配。
- 全局匹配时,返回单纯的数组,数组中不包含index和input属性。而在非全局匹配或字符串匹配时,包含这两个属性。
- 在匹配不到任意字符串时,得到null,且不再含有index和input属性。这应该是由于无法对null设置属性的原因。注:一般的数组上是可以设置属性的。
search方法
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
返回第一个与指定规则匹配的字符串的位置,如果不能匹配则返回-1。
search方法的使用
参数为字符串或正则表达式
'fjejfi'.search('j');//1'fjejfi'.search('h')//-1'hellowh'.search('h')//0'hellowh'.search(/h/g)//0
结论
- search方法在匹配不到字符时返回-1。
- search方法不care是不是全局匹配,它只负责第一次匹配。
replace方法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
replace用法
参数是(字符串/正则表达式,字符串/函数)。
'efejiejfief'.replace('e','4')//"4fejiejfief"'efejiejfief'.replace(/e/g,'4')//"4f4ji4jfi4f"'2014-08-09'.replace(/(\d+)-(\d+)-(\d+)/,'$2/$3/$1')//"08/09/2014"'2014-08-09'.replace(/(\d+)-(?:\d+)-(\d+)/,'$2/$3/$1')"09/$3/2014"'122233434'.replace('\\d+','h')//"122233434"'helloword'.replace('o',function(v){return '$'+v+'$'});//"hell$o$world"'helloworld'.replace('o',function(v,j){return '$'+j+'$'})//"hell$4$world"'helloworld'.replace('o',function(v,j,a){return '$'+a+'$'})//"hell$helloworld$world"'helloworld'.replace(/o/g,function(v,j,a){return '$'+v+'$'})//"hell$o$w$o$rld"'helloworld'.replace(/o/g,function(v,j,a){return '$'+a+'$'})//"hell$helloworld$w$helloworld$rld"'helloworld'.replace(/o/g,function(v,j,a){return '$'+j+'$'})//"hell$4$w$6$rld"
结论
- 如果是两个字符串,那么replace只会替换第一个匹配的字符
- 如果第一个参数是一个全局匹配的正则,那么replace将全部替换匹配的字符。
- 在replace方法中,我们可以使用
1、 2、3...这样的替代符来表示前面正则表达式中的捕获组。∗如果正则中的匹配组中含有非捕获组,那么该组并不会被捕捉,也不会在占用 n这样的替代符。 - 当前面一个参数是正则字符串时,replace并不会识别它为正则表达式,也不会对其进行转换,而是当作普通的字符串。
- 对于replace的第二个参数,如果是一个函数的话,那么函数的参数依次是匹配的字符串、所在位置、原字符串。
split方法
用于根据指定规则切分字符串成数组
split用法
参数为(字符串[,howmany])或(正则[,howmany])
'one1two2three3four4'.split(/\d/)//["one", "two", "three", "four", ""]'helloworld'.split('o')//["hell", "w", "rld"]'helloworld'.split('o',2)//["hell", "w"]'helloworld'.split('o',6)//["hell", "w", "rld"]'helloworld'.split('')//["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]helloworld'.split(/(\w)/)//["", "h", "", "e", "", "l", "", "l", "", "o", "", "w", "", "o", "", "r", "", "l", "", "d", ""]'helloworld'.split(/\w/)//["", "", "", "", "", "", "", "", "", "", ""]'helloworld'.split(/(\w+)/)//["", "helloworld", ""]'helloworld'.split(/\w+/)//["", ""]
结论
- 可以用第二个参数指定被切分后的最大数组长度。
- 如果正则中包含子表达式,那么结果数组中也将包含子表达式匹配的字符串。

浙公网安备 33010602011771号