Regexper,圖形化正則表達式

Regexper

在這裏插入圖片描述

/Win(?:dows)?\s?([^do]{2})\s?(\d+\.\d+)?/

在這裏插入圖片描述

元字符

元字符 說明
\ 轉義
. 匹配除換行符以外的任意字符
^ 匹配開頭
$ 匹配結尾
? 重複一次或零次
+ 重複一次或多次
* 重複零次或多次
| 多選分支
{n} 重複n次
{n,} 至少重複n次
{n,m} 至少重複n次,但不超過m次
[] 字符組
() 捕獲組

其他字符

- 說明
\d 匹配數字
\D 匹配非數字
\s 匹配空格
\S 匹配非空格
\w 匹配非特殊字符,比如大小寫字母、數字
\W 匹配特殊字符

開頭 和 轉義

  • /^\///開頭
    ^,匹配開頭
    \/,轉義爲/

在這裏插入圖片描述
var r = /^\//;

序號 測試 返回
1 "/".match(r) [ '/', index: 0, input: '/', groups: undefined ]
2 "/dist".match(r) [ '/', index: 0, input: '/dist', groups: undefined ]
3 "a/".match(r) null
4 "12".match(r) null

開頭 和 結尾

  • /^$/匹配空字符串
    ^,匹配開頭
    $,匹配結尾

    在這裏插入圖片描述

var r = /^$/;

序號 測試 返回
1 "".match(r) [ '', index: 0, input: '', groups: undefined ]
2 "a".match(r) null
3 "1".match(r) null

開頭 轉義 和 結尾

  • /^\/?$/,匹配空字符串和斜槓/
    ^,匹配開頭
    \/,轉義爲/
    $,匹配結尾

    在這裏插入圖片描述

var r = /^\/?$/

序號 測試 返回
1 "".match(r) [ '', index: 0, input: '', groups: undefined ]
2 "/".match(r) [ '/', index: 0, input: '/', groups: undefined ]
3 "//".match(r) null
4 "/dist".match(r) null
5 "abc".match(r) null

字符組

  • /[do]/匹配任意字符串,只要該字符串中包含字符do

    在這裏插入圖片描述

var r = /[do]/

序號 測試 返回
1 "d".match(r) [ 'd', index: 0, input: 'd', groups: undefined ]
2 "o".match(r) [ 'o', index: 0, input: 'o', groups: undefined]
3 "do".match(r) [ 'd', index: 0, input: 'do', groups: undefined]
4 "od".match(r) [ 'o', index: 0, input: 'od', groups: undefined]
5 "doo".match(r) [ 'd', index: 0, input: 'doo', groups: undefined]
6 "ood".match(r) [ 'o', index: 0, input: 'ood', groups: undefined]
7 "done".match(r) [ 'd', index: 0, input: 'done', groups: undefined]
8 "dad".match(r) [ 'd', index: 0, input: 'dad', groups: undefined]
9 "horse".match(r) [ 'o', index: 1, input: 'horse', groups: undefined]
10 "had".match(r) [ 'd', index: 2, input: 'had', groups: undefined]
11 "one".match(r) [ 'o', index: 0, input: 'one', groups: undefined]
12 "git".match(r) null

排除字符組

^[]內,表示要排除的字符組

  • /[^do]/匹配任意字符串,只要該字符串中包含除字符do以外的其他字符
    在這裏插入圖片描述

var r = /[^do]/

序號 測試 返回
1 "d".match(r) null
2 "o".match(r) null
3 "do".match(r) null
4 "od".match(r) null
5 "doo".match(r) null
6 "ood".match(r) null
7 "done".match(r) [ 'n', index: 2, input: 'done', groups: undefined]'
8 "dad".match(r) [ 'a', index: 1, input: 'dad', groups: undefined]
9 "horse".match(r) [ 'h', index: 0, input: 'horse', groups: undefined]
10 "had".match(r) [ 'h', index: 0, input: 'had', groups: undefined]
11 "one".match(r) [ 'n', index: 1, input: 'one', groups: undefined]
12 "git".match(r) [ 'g', index: 0, input: 'git', groups: undefined]

排除字符組 和 重複

^[]內,表示排除字符;{n},重複n

  • /[^do]{2}/匹配任意字符串,只要該字符串中至少包含兩個除字符do以外的其他字符
    在這裏插入圖片描述

var r = /[^do]{2}/

序號 測試 返回
1 "d".match(r) null
2 "o".match(r) null
3 "do".match(r) null
4 "od".match(r) null
5 "doo".match(r) null
6 "ood".match(r) null
7 "done".match(r) [ 'n', index: 2, input: 'done', groups: undefined]
8 "dad".match(r) null
9 "horse".match(r) [ 'rs', index: 2, input: 'horse', groups: undefined]
10 "had".match(r) [ 'ha', index: 0, input: 'had', groups: undefined]
11 "one".match(r) [ 'ne', index: 1, input: 'one', groups: undefined]
12 "git".match(r) [ 'gi', index: 0, input: 'git', groups: undefined]

捕獲組

  • /Win(dows)\s?([^do]{2})\s?(\d+\.\d+)?/
    (dows)是第一個捕獲組
    ([^do]{2})是第二個捕獲組
    (\d+\.\d+)是第三個捕獲組

在這裏插入圖片描述

  • string.match(r)
    返回一個類數組。其中,
    第一個元素是匹配的完整字符串,Windows NT 6.1
    第二個元素是與捕獲組(dows)匹配的內容,dows
    第三個元素是與捕獲組([^do]{2})匹配的內容,NT
    第四個元素是與捕獲組 (\d+\.\d+)匹配的內容,6.1
var r = /Win(dows)\s?([^do]{2})\s?(\d+\.\d+)?/;
console.log("Windows NT 6.1".match(r));
//['Windows NT 6.1','dows','NT','6.1',index: 0,input: 'Windows NT 6.1']
  • r.test(string)
    返回布爾值,true|false
    與捕獲組匹配的內容會保存在RegExp.$1~$9
    與第一個捕獲組(dows)匹配的內容保存在RegExp.$1
    與第二個捕獲組([^do]{2})匹配的內容保存在RegExp.$2
    與第三個捕獲組(\d+\.\d+)匹配的內容保存在RegExp.$3
var r = /Windows\s?([^do]{2})\s?(\d+\.\d+)?/;
r.test("Windows NT 6.1");//返回true
console.log(RegExp.$1,RegExp.$2,RegExp.$3);//輸出 dows NT 6.1 

非捕獲組

(?:)
非捕獲組(?:) 和 捕獲組()類似,區別是,它不會把 與其匹配的內容 保存起來,所以比較節省內存。

  • /Win(?:dows)\s?([^do]{2})\s?(\d+\.\d+)?/
    在這裏插入圖片描述
var r = /Win(?:dows)\s?([^do]{2})\s?(\d+\.\d+)?/;
console.log("Windows NT 6.1".match(r));
//['Windows NT 6.1','NT','6.1',index: 0,input: 'Windows NT 6.1']

r.test("Windows NT 6.1")
console.log(RegExp.$1,RegExp.$2,RegExp.$3);//NT 6.1 undefined
  • /industr(?:y|ies)/
    在這裏插入圖片描述

(?=)

  • /Win(?:dows)?\s?(?=98|NT)/
var r = /Win(?:dows)?\s?(?=98|NT)/;
console.log("WinNT".match(r));//[ 'Win', index: 0, input: 'WinNT']
console.log("Win 98".match(r));//[ 'Win ', index: 0, input: 'Win 98']
console.log("Windows NT 6.1".match(r));//[ 'Windows ', index: 0, input: 'Windows NT 6.1']
console.log(r.test("WinNT"));//輸出true
console.log(r.test("Win 98"));//輸出true
console.log(r.test("Windows NT 6.1"));//輸出true

在這裏插入圖片描述

  • /^\/?(?=\/|$)/
    匹配以下四種情況:
    • 空字符串
    • 斜槓/
    • /開頭的字符串
    • /開頭,且以/結尾的字符串
      在這裏插入圖片描述

參考文章

正則表達式高級用法(分組與捕獲)
非捕獲

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