JavaScript 之 正則表達式

一 分類

1. String支持的正則函數:

1)search()

search(regExp),直接量和對象,返回整數,開始的位置,-1表示沒有找到。

str = 'I word site excel word OK chrome match OK excel word';
//找word
console.log(str.search(/\bword\b/));

search 不支持全局,忽略參數g,同時忽略regexp的lastIndex

2)match(regexp),直接量和對象,

返回,沒有g,返回數組,0:第一個匹配的最大串,其他是分組的子串,index:最大串開始的位置, input原始的串

g,返回數組,所有匹配的最大串,但是沒有分組的子串,index和input無效。

3)replace(regexp/substr, replacement)

regexp直接量、對象。substr 子串

replacement,替換進去的串,或者函數。$1 ~ $99 分組,$&代表子串, $` 子串左側, $' 子串右側

把小寫的word變成大寫的WORD

console.log(str.replace(/\bword\b/, 'WORD'));

console.log(str.replace(/\bword\b/g, 'WORD'));

console.log(str.replace('word', 'WORD'));
// 匹配 單詞1-單詞2 模式,然後變成單詞2-單詞1
str = 'aaaa-bbbb@cc-ddd';
console.log(str.replace(/([a-z]+)-([a-z]+)/g, replacer));

function replacer(match, p1, p2, offset, str){
    //console.log(match, p1, p2, offset, str);
    var sRet = '';
    for(var i = 0; i < p2.length; i++){
        sRet += String.fromCharCode(p2.charCodeAt(i) + 1);
    }
    sRet += '=';
    for(var i = 0; i < p1.length; i++){
        sRet += String.fromCharCode(p1.charCodeAt(i) + 1);
    }
    return sRet;
}

// 結果
// aaaa-bbbb aaaa bbbb 0 aaaa-bbbb@cc-ddd
// controller11-6.js:196 cc-ddd cc ddd 10 aaaa-bbbb@cc-ddd
// controller11-6.js:193 cccc=bbbb@eee=dd

4)split(separator, howmany) 通過一個東西把字符串數組

separator:正則,字符串,可選

howmany是最大長度。

2.RegExp支持的正則函數

1)RegExp 實例屬性

lastIndex, global, ignoreCase, multiline, source,

2)RegExp 實例方法

(1)exce(string) :返回數組(找不到返回null)

  • 概念:該方法是爲捕獲組而專門設計的,該方法接收應用模式的字符串,返回一個數組且額外帶有index,input屬性。index 表示匹配項在字符串中的位置;input 是應用正則表達式的字符串。且數組第一項是和模式匹配的整個字符串;其他項是和模式中捕獲的組匹配的字符串。

var test = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;

var matches = pattern.exec(test);
console.log(matches[0]) // "mom and dad and baby"
console.log(matches[1]) // " and dad and baby"
console.log(matches[2]) // " and baby"
console.log(matches.index) // 0
console.log(matches.input) // "mom and dad and baby"
  • 有無 g 的區別

模式中設置 g:在同一字符串上多次調用 exec() 始終返回第一個匹配項的信息。

模式中不設置 g:在同一字符串上多次調用 exec() 會在字符串中繼續查找新的匹配項

var text = 'cat, bat, sat, fat';
var pattern1 = /.at/;

var matches = pattern1.exec(text);
console.log(matches.index); // 0
console.log(matches[0]); //cat
console.log(pattern1.lastIndex); // 0

matches = pattern1.exec(text); 
console.log(matches.index); // 0
console.log(matches[0]); //cat
console.log(pattern1.lastIndex); // 0

var pattern2 = /.at/gi
var matches1 = pattern2.exec(text); 
console.log(matches1.index); // 0
console.log(matches1[0]); //cat
console.log(pattern2.lastIndex); // 3

(2)test(string) :表示是否匹配,返回布爾值

var regExp = new RegExp('[0-9]+','g');  
// 或
var regExp2 = /[0-9]+/g; // 有g,匹配所有的字符串;沒有g,匹配完第一個然後停止。
var str = 123;
console.log(regExp2.test(str)); //true

在指向知道目標字符串和某個模式是否匹配,但不需要知道其文本內容的情況下使用。

3)RegExp 構造函數屬性

正則表達式字面量始終會共享同一個 RegExp 實例,而使用構造函數創建的每一個新RegExp 實例都是一個新實例。

RegExp構造函數上有一些屬性,並且這些屬性跟着執行的最後一次正則表達式操作而變化。

var text = 'this has been a short summer';
var pattern = /(.)hort/g;
if (pattern.test(text)) {
  console.log(RegExp.input); // this has been a short summer
  console.log(RegExp.leftContext); // this has been a 
  console.log(RegExp.rightContext); //  summer
  console.log(RegExp.lastMatch); // short
  console.log(RegExp.lastParen); // s
  console.log(RegExp.multiline); // false
}

RegExp.$1,…RegExp.$9,存儲捕獲的組。

 

二 種類

1. 簡單類:單詞本身

console.log(str.match(/hello/g));

2. 範圍類 []指代一個字符,裏面的字符表示這個被指代的字符的選擇範圍

例:十六進制 [0-9a-fA-F]

3. 負向類

例:[^0-9]不能是數字,排除數字

4. 量詞 一個前面單位出現的次數

console.log(str.match(/[0-9]{4}/g));

{n} 出現n次

{m,n} 至少m次,至多n次

{m,} 至少m次,至多不限

{0,} 0次或者多餘0次 => *

{1,} 1次或者多餘1次 => + 

{0,1} 0次或者1次, => ?

貪婪量詞: ? + * 找到匹配的最大串 //直到最後纔是停止

惰性量詞: *? +? 找到匹配的最小串 //是就停止

5. 通配符 預定義類

. = [^\n\r] 除了回車換行以外,都可以匹配。

\d = [0-9] 數字

\D = [^0-9] 非數字

\w = [a-zA-Z_0-9] 數字字母下劃線

\W = [^a-zA-Z_0-9] 非數字字母下劃線

\s = [\t\n\x0B\f\r ] 空格或者空白

\S 非空格或者非空白

\b 表示邊界(兩個字符中間的位置) 一邊是\w,另一邊是\W

\B 不是邊界。

^ 表示開始

$ 表示結束

6,分組 一個正則表達式,不但可以對整個匹配進行操作,還可以對其中()中的子串進行匹配,分組。

1)(pattern) 匹配pattern同時捕獲結果,自動設定組好。

\1 或 RegExp.$1 : 反向引用

2)(?<name>pattern) 匹配pattern同時捕獲結果,設定那麼爲組名。

\k<name>: 反向引用

var str = 'word excel excel hello world world!';

console.log(str.match(/(\b[a-zA-Z]+\b)\s+\1/g));

console.log(str.match(/(?<n1>\b[a-zA-Z]+\b)\s+\k<n1>/));

console.log(RegExp.$1);

7. 零寬(負向)先行\後行斷言

開頭是:(?<=什麼)  開頭不是:(?<!=什麼)

結尾是:(?=什麼)    結尾不是:(?!=什麼)

str = 'r;e;a;a;a;rcaaa=bbb=;';

// 尋找兩個字符,兩字符後面以分號結尾

console.log(str.match(/.{2}(?=;)/g));

// 尋找四個字符,以ea開頭

console.log(str.match(/(?<=ea).{4}/g));

// 尋找三個字母,不以分號結尾 

console.log(str.match(/[a-z]{3}(?!;)/g));

// 尋找不易re開頭的三個字母

console.log(str.match(/(?<!re)a{3}/));

8. 或 |

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章