JavaScript RegExp 對象

// 1 建立正則表達式兩種方式
// 1.1 用new
// 第1個參數模式是必須的 第2個模式修飾符參數可選
// i:忽略大小寫 g:全局匹配 m:多行匹配
var box = new RegExp('Box');
var box = new RegExp('Box', 'gim');

// 1.2 用字面量的形式
// 兩個反斜槓是正則表達式的字面量表示造成
var box = /Box/;
var box = /Box/gim;

// 2 使用正則表達式兩種方式
// 2.1 test()
/*
 * 只查找最多一個匹配,若是找到就當即返回true,不然返回false
 *
 * 若是爲正則表達式設置了全局標誌(g),test()函數仍然只查找最多一個匹配,
 * 不過咱們再次調用該對象的test()函數將會從regExpObject.lastIndex索引位置處開始查找,從而找到下一個匹配。
 * 若是想要從新使用test()函數從頭開始查找,則須要手動將regExpObject.lastIndex的值重置爲 0。
 * 
 * regExpObject.lastIndex 本次匹配內容的最後一個字符的下一個索引位置。關閉全局 regExpObject.lastIndex 的值永遠爲 0。
 */
var pen = /Pen/i;
pen.test('pen'); // true
pen.test('pencil'); // true
pen.test('box'); // false

var pattern = /google/g;
var str = 'google google google';
pattern.test(str); // true
pattern.lastIndex; // 6
pattern.test(str); // true
pattern.lastIndex; // 13
pattern.lastIndex = 0;
pattern.test(str); // true
pattern.lastIndex; // 6

var pattern = /google/;
var str = 'google google google';
pattern.test(str); // true
pattern.lastIndex; // 0

// 2.2 exec()
/*
 * 若是 exec() 找到了匹配的文本,則返回一個結果數組。
 * 此數組的第 0 個元素是與正則表達式相匹配的文本,
 * 第 1 個元素是與 RegExpObject 的第 1 個子表達式相匹配的文本(若是有的話),
 * 第 2 個元素是與 RegExpObject 的第 2 個子表達式相匹配的文本(若是有的話),以此類推。
 * RegExpObject 的 lastIndex 屬性爲匹配文本的最後一個字符的下一個位置。
 *
 * 若是 exec() 找不到匹配的文本,返回 null。lastIndex 屬性重置爲 0。
 * 
 */
var apple = /Apple/gi;
apple.exec('apple'); // ["apple"]
apple.exec('pineapple'); // ["apple"]
apple.exec('apple and pineapple'); // ["apple"]
apple.exec('pear'); // null

var email = /(\w+)@(\w+)\.(\w+)/g;
email.exec("zhang@yeah.net and dan@qq.com"); // ["zhang@yeah.net", "zhang", "yeah", "net"]
email.lastIndex; // 14
email.exec("zhang@yeah.net and dan@qq.com"); // ["dan@qq.com", "dan", "qq", "com"]
email.lastIndex; // 29
email.exec("zhang@yeah.net and dan@qq.com"); // null
email.lastIndex; // 0

// 3 使用String對象的正則表達式四種方法
// 3.1 match()
var bus = /Bus/i;
'bus'.match(bus); // ["bus"]
'Buses'.match(bus); // ["Bus"]
'This is a bus, it is my bus.'.match(bus); // ["bus"]
'These are buses, they are our buses.'.match(/Bus/gi); // ["bus", "bus"]
'car'.match(bus); // null

// 3.2 search()
var flower = /flower/ig;
'flower'.search(flower); // 0
'red flower and pink flower'.search(flower); // 4
'tree'.search(flower); // -1

// 3.3 replace(search, replacement)
// search 爲正則表達式時 replacement可使用 $一、$二、...、$9九、$&、$`、$'、$$ 變量來動態肯定用於替換的字符串內容
'stronger and stronger'.replace(/stronger/i, 'more'); // "more and stronger"
'stronger and stronger'.replace(/stronger/ig, 'more'); // "more and more"
'stronger and stronger'.replace(/and/, "$$"); // "stronger $ stronger"
'baidu google'.replace(/(.*)\s(.*)/, '$2 $1'); // "google baidu"

// 3.4 split()
var and = /And/i;
'pen And pencil and rule'.split(and); // ["pen ", " pencil ", " rule"]
'pen'.split(and); // ["pen"]
'and pencil'.split(and); // ["", " pencil"]
'pen and'.split(and); // ["pen ", ""]

// 4 正則表達式靜態屬性
// 全部靜態屬性只可讀 匹配失敗值不變 匹配成功值改變

// 4.1 RegExp.$1 - RegExp.$9
// 正則表達式是支持分組捕獲的,最前面的9個匹配文本會依次存儲在RegExp的靜態屬性$1-$9中。
/(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)/.test('a-b-c-d-e-f-g-h-i-j-k-l-m-n-o'); // true
RegExp.$1; // "a"
RegExp['$1']; // "a"
RegExp.$2;; // "b"
RegExp['$2']; // "b"
// ...
RegExp.$9; // "i"
RegExp['$9']; // "i"

// 4.2 RegExp.input
// 被匹配字符串 也能夠表示成 RegExp['$_'] 或者 RegExp.$_
/pen/.test("pen and pencil"); // true
RegExp.input; // "pen and pencil"
RegExp['$_']; // "pen and pencil"
RegExp.$_; // "pen and pencil"

// 4.3 RegExp.leftContext
// 當前成功匹配文本以前的全部字符 也能夠表示成 RegExp["$`"]
var pen = /pen/g;
pen.test("A pen and a pencil"); // true
RegExp.leftContext; // "A "
RegExp["$`"]; // "A "
pen.test("A pen and a pencil"); // true
RegExp.leftContext; // "A pen and a "
RegExp["$`"]; // "A pen and a "

// 4.4 RegExp.rightContext
// 當前成功匹配文本以後的全部字符 也能夠表示成 RegExp["$'"]
var pen = /pen/g;
pen.test("A pen and a pencil"); // true
RegExp.rightContext; // " and a pencil"
RegExp["$'"]; // " and a pencil"
pen.test("A pen and a pencil"); // true
RegExp.rightContext; // "cil"
RegExp["$'"]; // "cil"

// 4.5 RegExp.lastMatch
// 最後匹配到的字符串 也能夠表示成 RegExp['$&']
var pen = /pen/g;
pen.test("A pen and a pencil"); // true
RegExp.lastMatch; // "pen"
RegExp['$&']; // "pen"

// 4.6 RegExp.lastParen
// 最後帶括號的子匹配 也能夠表示成 RegExp["$+"]
/(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)/.test('a-b-c-d-e-f-g-h-i-j-k-l-m-n-o'); // true
RegExp.lastParen; // "j"
RegExp["$+"]; // "j"

// 5 正則表達式實例屬性
// 5.1 regExpObject.global
// 正則表達式是否具備global標誌
/google/.global; // false
/google/gim.global; // true

// 5.2 regExpObject.ignoreCase
// 正則表達式是否具備ignoreCase標誌
/google/.ignoreCase; // false
/google/gim.ignoreCase; // true

// 5.3 regExpObject.multiline
// 正則表達式是否具備multiline標誌
// IE不支持multiline屬性 不少的瀏覽器也不支持該屬性
/google/.multiline; // false
/google/gim.multiline; // true

// 5.4 regExpObject.source
// 正則表達式模式的文本內容
/google/.source; // "google"
/google/gim.source; // "google"

// 6 正則表達式模式
// ^n 以 n 開頭
// n$ 以 n 結束

// n* 零個或多個 n 
var patter = /go*gle/;
patter.test("ggle"); // true
patter.test("gogle"); // true
patter.test("google"); // true

// n+ 至少一個 n 
var patter = /go+gle/;
patter.test("ggle"); // false
patter.test("gogle"); // true
patter.test("google"); // true

// n? 零個或一個 n 
var patter = /go?gle/;
patter.test("ggle"); // true
patter.test("gogle"); // true
patter.test("google"); // false

// n{X} X 個 n
var patter = /go{2}gle/;
patter.test("gogle"); // false
patter.test("google"); // true
patter.test("gooogle"); // false

// n{X,} 至少 X 個 n
var patter = /go{2,}gle/;
patter.test("gogle"); // false
patter.test("google"); // true
patter.test("gooogle"); // true

// n{X,Y} 至少 X 個 n,最多 Y 個 n。
var patter = /go{2,3}gle/;
patter.test("gogle"); // false
patter.test("google"); // true
patter.test("gooogle"); // true
patter.test("goooogle"); // false

// ? 緊跟在*,+,?,{n},{n,},{n,m})任何一個後面時,匹配模式是非貪婪的。
// 非貪婪模式儘量少的匹配所搜索的字符串,而默認的貪婪模式則儘量多的匹配所搜索的字符串。
/[a-z]+/.exec('abcdef'); // ["abcdef"]
/[a-z]+?/.exec('abcdef'); // ["a"]

var greed = /[a-z]+/g;
greed.exec('abcdef'); // ["abcdef"]
greed.exec('abcdef'); // null
var lazy = /[a-z]+?/g;
lazy.exec('abcdef'); // ["a"]
lazy.exec('abcdef'); // ["b"]
lazy.exec('abcdef'); // ["c"]
lazy.exec('abcdef'); // ["d"]
lazy.exec('abcdef'); // ["e"]
lazy.exec('abcdef'); // ["f"]
lazy.exec('abcdef'); // null

'abcdef'.replace(/[a-z]+/, '貪婪'); // "貪婪"
'abcdef'.replace(/[a-z]+?/, '惰性'); // "惰性bcdef"

'abcdef'.replace(/[a-z]+/g, '貪婪'); // "貪婪"
'abcdef'.replace(/[a-z]+?/g, '惰性'); // "惰性惰性惰性惰性惰性惰性"

/[a-z]{3,6}/.exec('abcdef'); // ["abcdef"]
/[a-z]{3,6}?/.exec('abcdef'); // ["abc"]

// . 一個任意字符,除了換行和行結束符。
var patter = /g.gle/;
patter.test("ggle"); // false
patter.test("gogle"); // true
patter.test("g2gle"); // true
patter.test("google"); // false

// (pattern) 捕獲性分組 匹配pattern並獲取這一匹配
/(\d+)([a-z]+)(\d+)/.exec('123abc456'); // ["123abc456", "123", "abc", "456"]

// (pattern(pattern)) 嵌套分組 從外往內捕獲
/(a?(b?(c?)))/.exec('abc'); // ["abc", "abc", "bc", "c"]

// (?:pattern) 非捕獲性分組 匹配pattern但不獲取匹配結果
/(\d+)(?:[a-z]+)(\d+)/.exec('123abc456'); // ["123abc456", "123", "456"]

// (?=pattern) 正向確定預查 非獲取匹配
var pattern = /goo(?=gle)/;
pattern.exec('good'); // null
pattern.exec('google'); // ["goo"]
RegExp.$1; // ""

var pattern = /pe(?=n)/g;
var str = 'a pen and a pencil';
pattern.exec(str); // ["pe"]
pattern.lastIndex; // 4
pattern.exec(str); // ["pe"]
pattern.lastIndex; // 14
pattern.exec(str); // null
pattern.lastIndex; // 0

// x|y 匹配x或y

// [abc] 方括號之間的任何一個字符,方括號內的字符能夠是任何字符或字符範圍。
var patter = /g[0-9a-zAB]gle/;
patter.test("ggle"); // false
patter.test("gogle"); // true
patter.test("g2gle"); // true
patter.test("gAgle"); // true
patter.test("gBgle"); // true
patter.test("google"); // false
patter.test("gABgle"); // false

// [A-Z] A-Z中的任意一個字符
// [a-z] a-z中的任意一個字符
// [A-z] A-z中的任意一個字符
// [0-9] 0-9中的任意一個數字
// [^abc] 不在方括號之間的任意一個字符
var patter = /g[^12]gle/;
patter.test("ggle"); // false
patter.test("gogle"); // true
patter.test("g2gle"); // false
patter.test("google"); // false
// \b 一個單詞邊界
// \B 一個單詞非邊界
// \d 一個數字
// \D 一個非數字
// \s 匹配任何空白字符,包括空格、製表符、換頁符等等。
// \S 匹配任何非空白字符。
// \w 一個單詞字符,包括:a-z、A-Z、0-9,以及下劃線_ 。 // \W 一個非單詞字符

正則表達式速查表 html

相關文章
相關標籤/搜索