淺談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構造函數創建正則表達式時,第一個參數可以是字符串,也可以是直接創建的正則表達式。

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

例如:

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.在非特殊字符前加反斜杠表示下一個字符是特殊的;

2.將其后的特殊字符轉譯為字面量;

注意:在使用RegExp構造函數時要將\轉譯,因為\在字符串里也是轉譯字符

1.匹配輸入的開始;

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 匹配字符串的后兩個單詞。

注意:\1、\2、\n 是用在正則表達式的匹配環節,在正則表達式的替換環節,則要使用像 $1、$2、$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輸入項

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM