說說正則表達式

正則表達式

 
 
 
 
 
var a=/\d/g;//a 的屬性global: trueignoreCase: falselastIndex: 0multiline: falsesource: "\d"

關於正則表達式的一些表示方法這裏不作說明....javascript

正則表達式的方法

compile方法

....實在想不通爲啥要這個方法java

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(),可是它返回指定的值,而不是字符串的位置。atom

match方法的使用

其參數是一個字符串或正則表達式.
找出匹配指定參數的字符串並組成數組返回
數組有兩個屬性,分別是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
結論:
  • 返回數組是任意匹配到指定規則的字符串,固然也包括原字符串自己。
  • 全局匹配時,會找出全部符合條件的字符串,而非全局匹配只能獲得最先的匹配。
  • 全局匹配時,返回單純的數組,數組中不包含index和input屬性。而在非全局匹配或字符串匹配時,包含這兩個屬性。
  • 在匹配不到任意字符串時,獲得null,且再也不含有index和input屬性。這應該是因爲沒法對null設置屬性的緣由。注:通常的數組上是能夠設置屬性的。

search方法

search() 方法用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。
返回第一個與指定規則匹配的字符串的位置,若是不能匹配則返回-1。code

search方法的使用

參數爲字符串或正則表達式orm

 
 
 
 
 
'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 二、 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+/)//["", ""]
結論
  • 能夠用第二個參數指定被切分後的最大數組長度。
  • 若是正則中包含子表達式,那麼結果數組中也將包含子表達式匹配的字符串。


相關文章
相關標籤/搜索