淺談js中的正則表達式

不少時候多會被正則表達式搞的暈頭轉向,最近抽出時間對正則表達式進行了系統的學習,整理以下:正則表達式

正則表達式的建立

兩種方法,一種是直接寫,由包含在斜槓之間的模式組成;另外一種是調用RegExp對象的構造函數。函數

兩種方法的建立代碼以下:學習

// 直接建立
const regex1 = /ab+c/;
const regex2 = /^[a-zA-Z]+[0-9]*\W?_$/gi;

// 調用構造函數
const regex3 = new RegExp('ab+c');
const regex4 = new RegExp(/^[a-zA-Z]+[0-9]*\W?_$/, "gi");
const regex5 = new RegExp('^[a-zA-Z]+[0-9]*\W?_$', 'gi');

能夠看出,調用RegExp構造函數建立正則表達式時,第一個參數能夠是字符串,也能夠是直接建立的正則表達式。spa

須要注意的是:RegExp實例繼承的toLocaleString()和toString)()方法都會返回正則表達式的字面量,與建立正則表達式的方式無關code

例如:對象

const ncname = '[a-zA-Z_][\\w\\-\\.]*';
const qnameCapture = '((?:' + ncname + '\\:)?' + ncname + ')';
const startTagOpen = new RegExp('^<' + qnameCapture);
startTagOpen.toString();        // '/^<((?:[a-zA-Z_][\w\-\.]*\:)?[a-zA-Z_][\w\-\.]*)/'

 

正則表達式中的特殊字符

\ (反斜槓)

1.在非特殊字符前加反斜槓表示下一個字符是特殊的;blog

2.將其後的特殊字符轉譯爲字面量;繼承

注意:在使用RegExp構造函數時要將\轉譯,由於\在字符串裏也是轉譯字符字符串

1.匹配輸入的開始;input

2.在[]中的第一位時表示反向字符集;

例子:

 

/^A/.exec('an A')        // null
/^A/.exec('An E')        // ["A", index: 0, input: "An E"]

 

匹配輸入的結束

/t$/.exec('eater')        // null
/t$/.exec('eat')          // ["t", index: 2, input: "eat"]

*,  +,  .(小數點) 

*:匹配前一個表達式0次或屢次。等價於 {0,};

+:匹配前面一個表達式1次或者屢次。等價於 {1,};

.:匹配除換行符以外的任何單個字符;

? (問號)

1.匹配前面一個表達式0次或者1次。等價於 {0,1};
2.若是緊跟在任何量詞 * + ? {} 的後面,將會使量詞變爲非貪婪的(匹配儘可能少的字符),和缺省使用的貪婪模式正好相反;
3.運用於先行斷言

例子:

/\d+/.exec('123abc')           // ["123", index: 0, input: "123abc"]
/\d+?/.exec('123abc')            // ["1", index: 0, input: "123abc"]

(x)

匹配 'x' 而且記住匹配項,括號表示捕獲括號;

例子:

/(foo) (bar) \1 \2/.test('bar foo bar foo');   // false
/(bar) (foo) \1 \2/.test('bar foo bar foo');   // true
/(bar) (foo) \1 \2/.test('bar foo');           // false
/(bar) (foo) \1 \2/.test('bar foo foo bar');   // false
/(bar) (foo) \2 \1/.test('bar foo foo bar');   // true

'bar foo bar foo'.replace( /(bar) (foo)/, '$2 $1' );    // "foo bar bar foo"

模式 /(foo) (bar) \1 \2/ 中的 '(foo)' 和 '(bar)' 匹配並記住字符串 "foo bar foo bar" 中前兩個單詞。模式中的 \1 和 \2 匹配字符串的後兩個單詞。

注意:\一、\二、\n 是用在正則表達式的匹配環節,在正則表達式的替換環節,則要使用像 $一、$二、$n 這樣的語法。例如,'bar foo'.replace( /(...) (...)/, '$2 $1' )。

(?:x) 

匹配 'x' 可是不記住匹配項,這種叫做非捕獲括號;

例子:

'foo'.match(/foo{1,2}/)                // ["foo", index: 0, input: "foo"]
'foo'.match(/(?:foo){1,2}/)            // ["foo", index: 0, input: "foo"]
'foofoo'.match(/(?:foo){1,2}/)         // ["foofoo", index: 0, input: "foofoo"]
'foofoo'.match(/foo{1,2}/)             // ["foo", index: 0, input: "foofoo"]

使用場景:示例表達式 /(?:foo){1,2}/。若是表達式是 /foo{1,2}/,{1,2}將只對 ‘foo’ 的最後一個字符 ’o‘ 生效。若是使用非捕獲括號,則{1,2}會匹配整個 ‘foo’ 單詞。

x(?=y), x(?!y), x|y 

x(?=y):匹配'x'僅僅當'x'後面跟着'y';

x(?!y):匹配'x'僅僅當'x'後面不跟着'y';

x|y: 匹配x或y

這兩種匹配的結果都不包含y

例子:

'JackSprat'.match(/Jack(?=Sprat)/)            // ["Jack", index: 0, input: "JackSprat"]
'JackWprat'.match(/Jack(?=Sprat)/)            // null
'JackWprat'.match(/Jack(?=Sprat|Wprat)/)    // ["Jack", index: 0, input: "JackWprat"]
/\d+(?!\.)/.exec("3.141")        // ["141", index: 2, input: "3.141"]

{n}, {n,m}:

{n}:匹配了前面一個字符恰好發生了n次;

{n,m}:匹配前面的字符至少n次,最多m次。若是 n 或者 m 的值是0, 這個值被忽略;

例子:

    /a{2}/.exec('candy')         // null
    /a{2}/.exec('caandy')        // ["aa", index: 1, input: "caandy"]
    /a{2}/.exec('caaandy')       // ["aa", index: 1, input: "caaandy"]

    /a{1,3}/.exec('candy')       // ["a", index: 1, input: "candy"]
    /a{1,3}/.exec('caandy')      // ["aa", index: 1, input: "caandy"]
    /a{1,3}/.exec('caaandy')     // ["aaa", index: 1, input: "caaandy"]
    /a{1,3}/.exec('caaaandy')    // ["aaa", index: 1, input: "caaaandy"]
    

[xyz], [^xyz] 

[xyz]:一個字符集合。匹配方括號的中任意字符;

[^xyz]:一個反向字符集。匹配任何沒有包含在方括號中的字符;

這兩種匹配均可以使用破折號(-)來指定一個字符範圍,特殊符號在字符集中沒有了特殊意義。

例:

function escapeRegExp(string){
    return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$&"); 
    //$&表示整個被匹配的字符串
}

例子中的.*+?^=!:${}()都表示字面量,並無特殊意義。

其餘

\b:匹配一個詞的邊界。一個匹配的詞的邊界並不包含在匹配的內容中。換句話說,一個匹配的詞的邊界的內容的長度是0;

\B: 匹配一個非單詞邊界;

例子:

    /\bm/.exec('moon')                    // ["m", index: 0, input: "moon"]
    /\bm/.exec('san moon')                // ["m", index: 4, input: "san moon"]
    /oo\b/.exec('moon')                   // null
    /\B../.exec('noonday')                    // ["oo", index: 1, input: "noonday"]
    /y\B../.exec('possibly yesterday')        // /y\B../.exec('possibly yesterday')

\d:匹配一個數字,等價於[0-9];

\D:匹配一個非數字字符,等價於[^0-9];

\f:匹配一個換頁符 (U+000C);

\n:匹配一個換行符 (U+000A);

\r:匹配一個回車符 (U+000D);

\s:匹配一個空白字符,包括空格、製表符、換頁符和換行符,等價於[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff];

\S:匹配一個非空白字符,等價於[^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff];

\w:匹配一個單字字符(字母、數字或者下劃線),等價於[A-Za-z0-9_];

\W:匹配一個非單字字符,等價於[^A-Za-z0-9_];

正則表達式標誌

g:全局搜索;

i:不區分大小寫;

m:多行搜索;

正則表達式使用

RegExp有exec()和test()方法;

exec匹配的結果爲:匹配結果、捕獲結果,index和input。

test匹配的結果爲true或false,效率比exec要高。

String有match(),replace(),search(),split()方法;

match匹配的結果同RegExp的exec,replace根據正則表達式替換,search查找因此位置,split根據正則表達式分割字符串。

其中,當replace有function時,參數說明以下:

* 匹配項* 記憶項(括號裏面的項)* ...* 匹配的index* input輸入項

相關文章
相關標籤/搜索