修飾符
修飾符描述
i執行對大小寫不敏感的匹配。
g執行全域性匹配(查詢所有匹配)。
m執行多行匹配。
元字元元字元
描述
.匹配除了換行符以外的任意字元
\s代表任意空白符(換行,製表,空格)
\s匹配任意非空字串
\b匹配單詞邊界,匹配單詞的開頭和結尾
\b匹配乙個非單詞邊界
\d匹配乙個數字[0-9]
\d匹配乙個數字,等價於[ ^ 0-9]
\w匹配乙個單字字元
\w匹配乙個非單字字元
量詞量詞
描述^n
匹配字串的開始,用括號裡面表示排除
n$匹配字串的結束
n+匹配任何包含至少乙個 n 的字串。
n*匹配任何包含零個或多個n的字串
n?匹配任何包含零個或乙個n的字串
支援正規表示式的 string 物件的方法
方法描述
search
檢索與正規表示式相匹配的值,返回字串中開始位置
match
找到乙個或多個正規表示式的匹配。
replace
替換與正規表示式匹配的子串。
split
把字串分割為字串陣列。
regexp 物件方法
方法描述
compile
編譯正規表示式。
exec
檢索字串中指定的值。返回找到的值,並確定其位置。
test
檢索字串中指定的值。返回 true 或 false。
例子test
test檢測句子中是否能匹配到字元
const test =
newregexp
('hello world'
,'ig');
console.
log(test.
test
('hello world'))
;// true
execexec 返回的是陣列,有就返回陣列的值,沒有返回為null
const test1 =
newregexp
('hello world'
,'ig');
console.
log(test1.
exec
(' hello world'))
;// null
matchmatch(pattern) 將所有匹配的字串組合成陣列返回
const pattern=
/box/ig
;const str=
"this is a box! the is a box!"
;console.
log(str.
match
(pattern)
);
searchsearch(pattern) 返回字串中pattern開始位置,忽略全域性匹配
const pattern1=
/box/i
;const str1=
"this is a box! the is a box!"
;console.
log(str1.
search
(pattern1));
// 10
replacereplace(pattern) 替換匹配到的字串
const pattern2=
/box/ig
;const str2=
"this is a box! the is a box!"
;console.
log(str2.
replace
(pattern2,
'tom'))
;
splitsplit(pattern) 返回字串指定pattern拆分陣列
const pattern3 =
/ /ig
;//空格
const str3 =
"this is a box! the is a box!"
;console.
log(str3.
split
(pattern3));
//以空格進行分割,返回的是陣列
// 輸出結果
// [ 'this', 'is', 'a', 'box!', 'the', 'is', 'a', 'box!' ]
常用案例// 匹配模式
// \w表示a-za-z_ 錨元字元匹配(^ $) ^強制收匹配 $強制尾匹配,並且只匹配乙個
const pattern4=
/^\woogle\d$/
;// const pattern4=/^[a-z]oogle\d$/;
const str4=
"aoogle2"
; console.
log(pattern4.
test
(str4));
// true
// 注意: ^符號在裡面表示 非 在外邊表示強制首匹配,並且只匹配乙個 要想匹配多個值,使用+
// \b表示到達邊界 , |表示匹配或選擇模式
;//匹配或選擇其中某個字元,不是相等,包含的意思
//返回true
const pattern6 =
/^[1-9][0-9]$/
;const str6 =
"122534"
;//共6位數,第一位不能為0
console.
log(pattern6.
test
(str6));
// true
// 壓縮包字尾名 \w等於a-za-z0-9_ 使用^限定從首字母匹配 .是特殊符號需要\n進行轉義
// |選擇符必須使用()進行分組
const pattern7 =
/^[\w]+\.(zip|gz|rar)$/
;const str7=
"a12_.zip"
;//檔名 字母_數字.zip,gz,rar
console.
log(pattern7.
test
(str7));
// true
//刪除多餘空格
//方法一: 使用replace只匹配乙個,所以使用+匹配多個
//匹配開頭乙個
var pattern8=
/^\s+/
;var str8=
" google "
;var result=str8.
replace
(pattern8,'')
;//匹配結尾乙個
pattern8=
/\s+$/
; result=result.
replace
(pattern8,'')
; console.
log(
'|'+result+
'|')
;// |google|
//方法二: (.+)貪婪模式,使用惰性模式,後面的空格不讓匹配
var pattern9=
/^\s+(.+?)\s+$/
;var str9=
" google "
; console.
log(
'9')
;var result=pattern9.
exec
(str9,'')
[1];
console.
log(result)
console.
log(
'|'+result+
'|')
;//方法三: (.+)貪婪模式,改為惰性模式,使用分組模式,只取匹配的內容
var pattern11=
/^\s+(.+?)\s+$/
;var str11=
" google "
;var result=str11.
replace
(pattern11,
'$1');
//使用分組模式
console.
log(str11.
match
(pattern11));
console.
log(
'|'+result+
'|')
;// |google|
//簡單郵箱驗證
var pattern22=
/^([\w\.\_]+)@([\w\_]+)\.([a-za-z])$/
;var str22=
; console.
log(pattern22.
test
(str));
// true
js數字正規表示式,js小數正規表示式
js數字正規表示式,js小數正規表示式 是否為數字 僅正數 包括正整數 正小數 0 param value returns function isnumber value 是否為所有數字 正數 負數 包括整數 小數 0 param value returns function isnumberall...
JS 正規表示式
驗證數字的正規表示式集 驗證數字 0 9 驗證n位的數字 d 驗證至少n位數字 d 驗證m n位的數字 d 驗證零和非零開頭的數字 0 1 9 0 9 驗證有兩位小數的正實數 0 9 0 9 驗證有1 3位小數的正實數 0 9 0 9 驗證非零的正整數 1 9 0 9 驗證非零的負整數 1 9 0 ...
js正規表示式
用正規表示式模式在字串中執行查詢,並返回包含該查詢結果的乙個陣列。rgexp.exec str 引數rgexp 必選項。包含正規表示式模式和可用標誌的正規表示式物件。str 必選項。要在其中執行查詢的string物件或字串文字。說明如果exec方法沒有找到匹配,則它返回null。如果它找到匹配,則e...