精簡《JavaScript高級程序設計》5、引用類型(RegExp)

《JavaScript高級程序設計》5、引用類型(RegExp)

RegExp類型

經過RegExp類型來支持正則表達式。html

let expression = /pattern / flags

其中的模式(pattern)部分能夠是任何簡單或複雜的正則表達式,能夠包含字符類、限定符、分組、向前查找以及反向引用。git

每個正則表達式均可帶有一或多個標誌(flags),用以代表正則表達式的行爲。web

正則表達式的匹配模式支持下列3個標誌正則表達式

  • g:表示全局(global)模式,即模式將被應用於全部字符串,而非在發現第一個匹配項時當即中止;
  • i:表示不區分大小寫(case-insensitive)模式,即在肯定匹配項時忽略模式與字符串的大小寫;
  • m:表示多行(multiline)模式,即在到達一行文本末尾時還會繼續查找下一行是否存在於模式匹配項

正則表達式 = 1個模式+ 3個標誌 (任意),不一樣的組合產生不一樣結果express

注意:默認狀況下,一個字符串不管是否換行,只要只有一個開始標誌和一個結束標誌$,若是採用多行匹配,那麼每一行都有一個和&做爲開始和結束。數組

基本使用:svg

//匹配字符串全部「fur」的實例
pattern1 =  /fur/g ;
//匹配第一個字符串「fur」,不區分大小寫
pattern2 =  /fur/i ;
//匹配全部「fur」實例且不區分大小寫。
pattern3 =  /fur/gi ;
console.log(pattern1.test("catfur"))//true
console.log(pattern2.test("catFur"))//true
console.log(pattern3.test("catFur"))//true

注意:模式中使用的全部元字符都必須轉義,包括:( { \ ^ $ | ) * + . ] },由於這些元字符的正則表達式中都有特殊用途函數

例如:grunt

//匹配第一個fur或cur,不區分大小寫
let p1 =  /[fc]ur/i;
//匹配第一個"[fc]ur",不區大小寫,轉義有特殊用途的[]
let p2 =  /\[fc\]ur/i ;
console.log(p1.test("catFur"))//true
console.log(p2.test("cat[Fc]Ur"))//true

除了用字面量定義正則表達式,還能夠經過RegExp構造函數,接收兩個參數:一個是匹配的字符串,一個是可選的標誌字符串。spa

例如:

let p1 = new RegExp("[fc]ur","i")
console.log(p1.test("catFur"))//true

注意:因爲構造函數的模式參數都是字符串,因此全部元字符都必須進行雙重轉義即\\,轉義過的字符也是如此。

let p1 = new RegExp("\\[fc\\]ur","i")
console.log(p1.test("cat[Fc]ur"))//true

RegExp實例屬性

global:布爾值,表示是否設置了g標誌

ignoreCase:布爾值,表示是否設置了i標誌

lastIndex:整數,表示開始搜索下一個匹配項的字符位置,從0算起。

multiline:布爾值,表示是否設置了m標誌

source:正則表達式的字符串表示,按照字面量形式而非傳入構造函數中的字符串模式返回

例如:

let p =  /\[fc\]ur/i;
console.log(p.ignoreCase)//true
console.log(p.lastIndex)//0
console.log(p.source)//\[fc\]ur

RegExp實例方法

exec()該方法專門爲捕獲組設計。接受一個參數,即要應用模式的字符串,而後返回包含第一個匹配項信息的數組,或者沒有匹配項時返回null。

返回的數組雖然是Array的實例,可是包含index和input兩個額外屬性,index表示匹配項字符串的位置,input表示應用正則表達式的字符串。

在數組中,第一項是與整個模式匹配的字符串,其餘項是與模式中的捕獲組匹配的字符串。(若是沒有捕獲組,則只有第一項)

例如:

let text = "fur like study and play"
let p =  /fur( like study( and play)?)?/gi;
let matches = p.exec(text)
console.log(matches.index)//0
console.log(matches.input)//fur like study and play
console.log(matches[0])//fur like study and play
console.log(matches[1])// like study and play
console.log(matches[2])// and play

對於exec()方法,即便設置了全局標誌,每次頁只會返回第一個匹配項,不過會繼續找新的匹配項;可是若是不設置全局標誌,則會在同一個字符串上屢次調用exec()將始終返回同一個結果。

let text = "fur is fur"

let p1 =  /fur/g;
let matches1 = p1.exec(text)
console.log(matches1.index)//0
console.log(matches1[0])//fur
console.log(p1.lastIndex)//3
matches1 = p1.exec(text)
console.log(matches1.index)//9
console.log(matches1[0])//fur
console.log(p1.lastIndex)//12

let p2 =  /fur/;
let matches2 = p2.exec(text)
console.log(matches2.index)//0
console.log(matches2[0])//fur
console.log(p2.lastIndex)//0
matches2 = p2.exec(text)
console.log(matches2.index)//0
console.log(matches1[0])//fur
console.log(p2.lastIndex)//0

test()接受一個字符串參數,在模式與該參數匹配狀況下返回true,不然返回false。經常使用於驗證輸入是否有效。

let text = "000-00-0000"
let p =  /\d{3}-\d{2}-\d{4}/;
console.log(p.test(text))//true

RegExp實例繼承的toString()toLocaleString()返回的是字符串,valueOf()返回的是正則表達式自己

let p = new RegExp("\\[fc\\]ur","i")
console.log(p.source)// [fc\]ur
console.log(p.toString())// \[fc\]ur/i
console.log(typeof(p.toLocaleString()))// \[fc\]ur/i
console.log((p.valueOf()) instanceof RegExp)//true

RegExp構造函數屬性

這些屬性適用於做用域中的全部正則表達式,而且基於所執行的最近一次正則表達式操做而變化。並且能夠經過兩種方式訪問他們。

長屬性名 短屬性名 說明
input $_ 最近一次要匹配的字符串
leftContext $` input字符串中lastMatch以前的文本
rightContext $’ Input字符串中lastMatch以後的文本
lastMatch $& 最近一次匹配的項
lastParen $+ 最近一次匹配的捕獲組
multiline $* 布爾值,表示是否全部表達式都使用多行模式
let text = "fur like play"
let p = /(.)ike/g;
if(p.test(text)){
    console.log(RegExp.$_)//fur like play
    console.log(RegExp["$`"])//fur 
    console.log(RegExp["$'"])// study
    console.log(RegExp["$&"])//like
    console.log(RegExp["$+"])//l
    console.log(RegExp["$*"])//undefined
}

multiline輸出結果與書本不合,以運行結果爲準

還有多達9個用來存儲捕獲組的構造函數屬性,RegExp["$1"]…RegExp["$9"]

let text = "fur like play"
let p = /(..)k(...)/g;
if(p.test(text)){
    console.log(RegExp["$1"])//li
    console.log(RegExp["$2"])//e p
}

擴展內容(來自菜鳥教程)

方括號:方括號用於查找某個範圍內的字符:
表達式 描述
[abc] 查找方括號之間的任何字符。
[^abc] 查找任何不在方括號之間的字符。
[0-9] 查找任何從 0 至 9 的數字。
[a-z] 查找任何從小寫 a 到小寫 z 的字符。
[A-Z] 查找任何從大寫 A 到大寫 Z 的字符。
[A-z] 查找任何從大寫 A 到小寫 z 的字符。
[adgk] 查找給定集合內的任何字符。
[^adgk] 查找給定集合外的任何字符。
(red|blue|green) 查找任何指定的選項。
元字符:元字符(Metacharacter)是擁有特殊含義的字符:
元字符 描述
. 查找單個字符,除了換行和行結束符。
\w 查找單詞字符。
\W 查找非單詞字符。
\d 查找數字。
\D 查找非數字字符。
\s 查找空白字符。
\S 查找非空白字符。
\b 匹配單詞邊界。
\B 匹配非單詞邊界。
\0 查找 NULL 字符。
\n 查找換行符。
\f 查找換頁符。
\r 查找回車符。
\t 查找製表符。
\v 查找垂直製表符。
\xxx 查找以八進制數 xxx 規定的字符。
\xdd 查找以十六進制數 dd 規定的字符。
\uxxxx 查找以十六進制數 xxxx 規定的 Unicode 字符。
量詞
量詞 描述
n+ 匹配任何包含至少一個 n 的字符串。 例如,/a+/ 匹配 「candy」 中的 「a」,「caaaaaaandy」 中全部的 「a」。
n* 匹配任何包含零個或多個 n 的字符串。例如,/bo*/ 匹配 「A ghost booooed」 中的 「boooo」,「A bird warbled」 中的 「b」,可是不匹配 「A goat grunted」。
n? 匹配任何包含零個或一個 n 的字符串。例如,/e?le?/ 匹配 「angel」 中的 「el」,「angle」 中的 「le」。
n{X} 匹配包含 X 個 n 的序列的字符串。 例如,/a{2}/ 不匹配 「candy,」 中的 「a」,可是匹配 「caandy,」 中的兩個 「a」,且匹配 「caaandy.」 中的前兩個 「a」。
n{X,} X 是一個正整數。前面的模式 n 連續出現至少 X 次時匹配。 例如,/a{2,}/ 不匹配 「candy」 中的 「a」,可是匹配 「caandy」 和 「caaaaaaandy.」 中全部的 「a」。
n{X,Y} X 和 Y 爲正整數。前面的模式 n 連續出現至少 X 次,至多 Y 次時匹配。 例如,/a{1,3}/ 不匹配 「cndy」,匹配 「candy,」 中的 「a」,「caandy,」 中的兩個 「a」,匹配 「caaaaaaandy」 中的前面三個 「a」。注意,當匹配 「caaaaaaandy」 時,即便原始字符串擁有更多的 「a」,匹配項也是 「aaa」。
n$ 匹配任何結尾爲 n 的字符串。
^n 匹配任何開頭爲 n 的字符串。
?=n 匹配任何其後緊接指定字符串 n 的字符串。
?!n 匹配任何其後沒有緊接指定字符串 n 的字符串。
支持正則表達式的 String 對象的方法
方法 描述 FF IE
search 檢索與正則表達式相匹配的值。 1 4
match 找到一個或多個正則表達式的匹配。 1 4
replace 替換與正則表達式匹配的子串。 1 4
split 把字符串分割爲字符串數組。 1 4

回顧《JavaScript高級程序設計》目錄

有用的話點個贊丫謝謝

相關文章
相關標籤/搜索