1. 程式人生 > >JS應用之正則表示式

JS應用之正則表示式

定義

正則表示式是用於匹配字串中字元組合的模式。

建立正則表示式

兩種方式:

1.new RegExp()


let pattern1 = new RegExp('cat'); //第一個引數字串
let pattern2 = new RegEXP('cat', 'ig'); //第二個引數可選模式修飾符
  • i:忽略大小寫
  • g:全域性匹配,即模式被應用於所有字串,而非匹配到第一項時立即停止
  • m:多行匹配
  • y:執行“粘性”搜尋,匹配從目標字串的當前位置開始,可以使用y標誌

2.字面量(以下栗子均使用字面量的方式建立正則表示式)


let pattern3 = /cat/;
let pattern4 = /cat/ig;

匹配正則表示式

1.test
在字串中測試是否匹配的RegExp方法,它返回true或false。


let str = 'This is a cat!';
console.log(pattern4.test(str)); //true

2.exec
在字串中執行查詢匹配的RegExp方法,它返回一個數組(未匹配到則返回null)。


console.log(pattern4.exec(str)); //[cat]

3.match
在字串中執行查詢匹配的String方法,它返回一個數組或者在未匹配到時返回null。


console.log(str.match(pattern4)); //[cat]

4.replace
在字串中執行查詢匹配的String方法,並且使用替換字串替換掉匹配到的子字串。


console.log(str.replace(pattern4, 'dog')); //This is a dog!

5.search
在字串中測試匹配的String方法,它返回匹配到的位置索引,或者在失敗時返回-1。


console.log(str.search(pattern4)); //10

6.split
使用正則表示式或者一個固定字串分隔一個字串,並將分隔後的子字串儲存到陣列中的String方法。


console.log(str.split(pattern4)); //["This is a ", "!"]

正則表示式語法

重複匹配(?、*、+、.、{m,n})

  • .匹配除換行符外的任意字元,單個匹配
  • x?匹配0個或1個x
  • x*匹配0個或任意多個x
  • x+匹配至少1個x
  • x{m,n}匹配最少m,最多n個的x,閉區間
  • (xyz){m,n}把xyz看成一個整體,匹配xyz最少m次最多n次
  • chrome|firefox|ie匹配chrome或者firefox或者ie中的任意一個

let str = 'google',
    str1 = 'gooooogle',
    str2 = 'ggle',
    pattern = /g..gle/,
    pattern1 = /go*gle/,
    pattern2 = /go+gle/,
    pattern3 = /g.*gle/,//0個或多個的任意字元
    pattern4 = /go?gle/,
    pattern5 = /go{2,4}gle/,
    pattern6 = /go{3}gle/,//匹配3個o->gooogle
    pattern7 = /go{3,}gle/;//匹配3個或3個以上o
    
console.log(pattern.test(str));//true
console.log(pattern1.test(str));//true
console.log(pattern1.test(str1));//true
console.log(pattern2.test(str1));//true
console.log(pattern2.test(str2));//false
console.log(pattern3.test(str));//true
console.log(pattern3.test(str2));//true
console.log(pattern4.test(str));//false
console.log(pattern7.test(str1));//true

字元類匹配

  • [a-z]*表示任意個a-z中的字元
  • [A-Z]*表示任意個A-Z中的字元
  • [0-9]*表示任意個0-9中的字元
  • [a-zA-Z0-9]表示匹配一個以上三種情況下的任意一個字元
  • [^0-9]表示非0-9的任意字元
  • ^[0-9]表示以0-9為起始字元,^表示從起始位置開始匹配
  • [0-9]$表示以0-9為結束字元,$表示匹配結束位置
  • /d匹配數字,同[0-9]
  • /D匹配非數字,同[ ^0-9]
  • /w匹配字母數字及下劃線_,同[a-zA-Z0-9_]
  • /W匹配非字母數字及下劃線_,同[^a-zA-Z0-9_]
  • \b匹配單詞邊界
  • \B匹配非單詞邊界

空白字元

  • \0匹配null字元
  • \f匹配換頁字元
  • \n匹配換行符
  • \r匹配回車字元
  • \t匹配製表符
  • \s匹配空白字元、空格、製表符和換行符
  • \S匹配非空白字元

貪婪模式和非貪婪模式
?緊跟在任何量詞 *、 +、? 或 {} 的後面,將會使量詞變為非貪婪的(匹配儘量少的字元),和預設使用的貪婪模式(匹配儘可能多的字元)正好相反。


console.log('123abc'.match(/\d+/)); //[123]
console.log('123abc'.match(/\d+?/)); //[1]

捕獲和非捕獲
(x)匹配 'x' 並且記住匹配項。括號被稱為 捕獲括號。


console.log(/(\d+)([a-z]+)/.exec('123abc')); //[12abc, 123, abc]
console.log(/(\d+)(?:[a-z]+)/.exec('123abc')); //[123abc, 123]

正向肯定查詢和正向否定查詢
x(?=y)匹配'x'僅僅當'x'後面跟著'y'.這種叫做正向肯定查詢。
x(?!y)匹配'x'僅僅當'x'後面不跟著'y',這個叫做正向否定查詢。


console.log(/goo(?=gle)/.exec('google')); //[goo]
console.log(/goo(?=gle)/.exec('goodu')); //null

console.log(/goo(?!gle)/.exec('google')); //null
console.log(/goo(?!gle)/.exec('goodu')); //[goo]

常用的正則表示式

1.手機號(1xxxxxxxxxx):/^1[0-9]{10}$/
2.郵政編碼校驗:/[1-9][0-9]{5}/
3.匹配漢字:[u4e00-u9fa5]
4.簡易郵箱校驗:/^([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_.-]+).([a-zA-Z]{2,4})$/

原文地址:https://segmentfault.com/a/1190000016879195