RegExp 是正則表達式的縮寫。正則表達式
當您檢索某個文本時,能夠使用一種模式來描述要檢索的內容。RegExp 就是這種模式。數組
簡單的模式能夠是一個單獨的字符。code
更復雜的模式包括了更多的字符,並可用於解析、格式檢查、替換等等。regexp
您能夠規定字符串中的檢索位置,以及要檢索的字符類型,等等。對象
const pattern = new RegExp('str');
test() 方法檢索字符串中的指定值。返回值是 true 或 false。ip
const pattern = new RegExp('str'); console.log(pattern.test('input string')); // true
exec() 方法檢索字符串中的指定值。返回值是被找到的值。若是沒有發現匹配,則返回 null。字符串
const pattern = new RegExp('str'); console.log(pattern.test('input string')); // [ 'str', index: 6, input: 'input string', groups: undefined ]
compile() 方法用於改變 RegExp(既能夠改變檢索模式,也能夠添加或刪除第二個參數)。input
const pattern = new RegExp('e'); console.log(pattern.test('The best things in life are free')); pattern.compile('d'); console.log(pattern.test('The best things in life are free'));
search()方法檢索與正則表達式相匹配的子字符串。search() 方法不執行全局匹配,它將忽略標誌 g。它同時忽略 regexp 的 lastIndex 屬性,而且老是從字符串的開始進行檢索,這意味着它老是返回 stringObject 的第一個匹配的位置。string
// stringObject.search(regexp) 語法 (匹配不上時返回 -1) const stringObj = 'Hello world'; const result = stringObj.search(/l{3}/); console.log(result); // -1
match() 方法可在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配。該方法相似 indexOf() 和 lastIndexOf(),可是它返回指定的值,而不是字符串的位置。it
// stringObject.match(regexp) 語法 (匹配不上時返回 null) const stringObj = 'Hello world'; const result = stringObj.match(/l{2}/); console.log(result); // [ 'll', index: 2, input: 'Hello world', groups: undefined ]
replace() 方法用於在字符串中用一些字符替換另外一些字符,或替換一個與正則表達式匹配的子串。
字符串 stringObject 的 replace() 方法執行的是查找並替換的操做。它將在 stringObject 中查找與 regexp 相匹配的子字符串,而後用 replacement 來替換這些子串。若是 regexp 具備全局標誌 g,那麼 replace() 方法將替換全部匹配的子串。不然,它只替換第一個匹配子串。
// stringObject.replace(regexp/substr,replacement) 語法 (匹配不上時返回原字符串) const stringObj = 'Hello world'; const result = stringObj.replace(/l{2}/,'ii'); console.log(result); // Heiio world
split() 方法用於把一個字符串分割成字符串數組。
返回一個字符串數組。該數組是經過在 separator 指定的邊界處將字符串 stringObject 分割成子串建立的。返回的數組中的字串不包括 separator 自身。
可是,若是 separator 是包含子表達式的正則表達式,那麼返回的數組中包括與這些子表達式匹配的字串(但不包括與整個正則表達式匹配的文本)。
// stringObject.split(separator,howmany) 語法 (匹配不上時返回 [stringObject]) const stringObj = 'Hello world'; const result = stringObj.split(/l{2}/); console.log(result) // [ 'He', 'o world' ]
/pattern/attributes
new RegExp(pattern, attributes);
參數 pattern 是一個字符串,指定了正則表達式的模式或其餘正則表達式。
參數 attributes 是一個可選的字符串,包含屬性 "g"、"i" 和 "m",分別用於指定全局匹配、區分大小寫的匹配和多行匹配。ECMAScript 標準化以前,不支持 m 屬性。若是 pattern 是正則表達式,而不是字符串,則必須省略該參數。
注意
const reg = /str/; const newReg = reg.compile(reg, 'i'); // 報錯 console.log(newReg.test('string')) // 正確示例 const reg = /str/; const newReg = new RegExp(reg, 'i'); console.log(newReg.test('string'));
修飾符 | 描述 |
---|---|
i | 執行對大小寫不敏感的匹配。 |
g | 執行全局匹配 |
m | 執行多行匹配 |
表達式 | 描述 |
---|---|
[abc] | 查找方括號之間的任何字符 |
1 | 查找任何不在方括號之間的任何字符 |
[0-9] | 查找任何從0至9的數字 |
[a-z] | 查找任何從小寫a至小寫z的字符 |
[A-Z] | 查找任何從大寫A至大寫Z的字符 |
[A-z] | 查找任何從大寫A至小寫z的字符 |
[adgk] | 查找給定集合內的任何字符 |
[^adgk] | 查找給定集合外的任何字符 |
(red ¦ blue ¦green) | 查找任何指定的選項 |
元字符 | 描述 |
---|---|
. | 查找單個字符,除了換行和行結束符。 |
w | 查找單詞字符。 |
W | 查找非單詞字符。 |
d | 查找數字字符。 |
D | 查找非數字字符。 |
s | 查找空白字符。 |
S | 查找非空白字符。 |
b | 匹配單詞邊界。 |
B | 匹配非單詞邊界。 |
0 | 查找 NUL 字符。 |
n | 查找換行符。 |
f | 查找換頁符。 |
r | 查找回車符。 |
t | 查找製表符。 |
v | 查找垂直製表符。 |
xxx | 查找以八進制數 xxx 規定的字符。 |
xdd | 查找以十六進制數 dd 規定的字符。 |
uxxxx | 查找以十六進制數 xxxx 規定的 Unicode 字符。 |
元字符 | 描述 |
---|---|
n+ | 匹配任何包含至少一個 n 的字符串。 |
n* | 匹配任何包含零個或多個 n 的字符串。 |
n? | 匹配任何包含零個或一個 n 的字符串。 |
n{X} | 匹配包含 X 個 n 的序列的字符串。 |
n{X,Y} | 匹配包含 X 至 Y 個 n 的序列的字符串。 |
n{X,} | 匹配包含至少 X 個 n 的序列的字符串。 |
n$ | 匹配任何結尾爲 n 的字符串。 |
^n | 匹配任何開頭爲 n 的字符串。 |
?=n | 匹配任何其後緊接指定字符串 n 的字符串。 |
?!n | 匹配任何其後沒有緊接指定字符串 n 的字符串。 |