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+/)//["", ""]