var a=/\d/g;//a 的属性global: trueignoreCase: falselastIndex: 0multiline: falsesource: "\d"
关于正则表达式的一些表示方法这里不作说明....javascript
....实在想不通为啥要这个方法java
检测给定的字符串是否符合该正则表达式规则。正则表达式
参数为字符串,若是匹配,返回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
- input: 对字符串参数的引用。
- lastMatch: 最近一次的匹配项
- leftContext: lastMatch左边的内容
- rightContext: lastMatch右边的内容
- lastParen: 最近一次匹配的捕获组,没有捕获组则为空
- multiline: 是否全部表达式都使用了多行模式,若是是则为true, 不然为false。
1− 9: 用于匹配对应的捕获组。
执行匹配,返回一个匹配的数组,若是没有匹配上则返回null。函数
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-"
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法相似 indexOf() 和 lastIndexOf(),可是它返回指定的值,而不是字符串的位置。atom
其参数是一个字符串或正则表达式.
找出匹配指定参数的字符串并组成数组返回
数组有两个属性,分别是index和input属性
index属性指定了匹配字符的起始字符在原字符串中的索引,input是对原字符串的引用。spa
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
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
返回第一个与指定规则匹配的字符串的位置,若是不能匹配则返回-1。code
参数为字符串或正则表达式orm
'fjejfi'.search('j');//1'fjejfi'.search('h')//-1'hellowh'.search('h')//0'hellowh'.search(/h/g)//0
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"
用于根据指定规则切分字符串成数组
参数为(字符串[,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+/)//["", ""]