不少时候多会被正则表达式搞的晕头转向,最近抽出时间对正则表达式进行了系统的学习,整理以下:javascript
两种方法,一种是直接写,由包含在斜杠之间的模式组成;另外一种是调用 RegExp
对象的构造函数。前端
两种方法的建立代码以下:java
// 直接建立
const regex1 = /ab+c/;
const regex2 = /^[a-zA-Z]+[0-9]*\W?_$/gi;
// 调用构造函数
const regex3 = new RegExp('ab+c');
const regex4 = new RegExp(/^[a-zA-Z]+[0-9]*\W?_$/, "gi");
const regex5 = new RegExp('^[a-zA-Z]+[0-9]*\W?_$', 'gi');
复制代码
能够看出,调用 RegExp
构造函数建立正则表达式时,第一个参数能够是字符串,也能够是直接建立的正则表达式。正则表达式
须要注意的是:RegExp
实例继承的 toLocaleString()
和 toString)()
方法都会返回正则表达式的字面量,与建立正则表达式的方式无关。函数
例如:学习
const ncname = '[a-zA-Z_][\\w\\-\\.]*';
const qnameCapture = '((?:' + ncname + '\\:)?' + ncname + ')';
const startTagOpen = new RegExp('^<' + qnameCapture);
// '/^<((?:[a-zA-Z_][\w\-\.]*\:)?[a-zA-Z_][\w\-\.]*)/'
startTagOpen.toString();
复制代码
\
(反斜杠)ui
注意:在使用 RegExp
构造函数时要将\转译,由于 \
在字符串里也是转译字符。spa
^
code
[]
中的第一位时表示反向字符集;例:regexp
/^A/.exec('an A') // null
/^A/.exec('An E') // ["A", index: 0, input: "An E"]
复制代码
$
匹配输入的结束
/t$/.exec('eater') // null
/t$/.exec('eat') // ["t", index: 2, input: "eat"]
复制代码
*
, +
, .
(小数点)
*
:匹配前一个表达式0次或屡次。等价于 {0,}
;
+
:匹配前面一个表达式1次或者屡次。等价于 {1,}
;
.
:匹配除换行符以外的任何单个字符;
?
(问号)
{0,1}
;* + ? {}
的后面,将会使量词变为非贪婪的(匹配尽可能少的字符),和缺省使用的贪婪模式正好相反;例子:
/\d+/.exec('123abc') // ["123", index: 0, input: "123abc"]
/\d+?/.exec('123abc') // ["1", index: 0, input: "123abc"]
复制代码
(x)
匹配 x
而且记住匹配项,括号表示捕获括号;
例:
/(foo) (bar) \1 \2/.test('bar foo bar foo'); // false
/(bar) (foo) \1 \2/.test('bar foo bar foo'); // true
/(bar) (foo) \1 \2/.test('bar foo'); // false
/(bar) (foo) \1 \2/.test('bar foo foo bar'); // false
/(bar) (foo) \2 \1/.test('bar foo foo bar'); // true
'bar foo bar foo'.replace( /(bar) (foo)/, '$2 $1' ); // "foo bar bar foo"
复制代码
模式 /(foo) (bar) \1 \2/
中的 (foo)
和 (bar)
匹配并记住字符串 foo bar foo bar
中前两个单词。模式中的 \1
和 \2
匹配字符串的后两个单词。
注意:\1
、\2
、\n
是用在正则表达式的匹配环节,在正则表达式的替换环节,则要使用像 $1
、$2
、$n
这样的语法。例如,'bar foo'.replace( /(...) (...)/, '$2 $1' )
。
(?:x)
匹配 x
可是不记住匹配项,这种叫做非捕获括号;
例:
'foo'.match(/foo{1,2}/) // ["foo", index: 0, input: "foo"]
'foo'.match(/(?:foo){1,2}/) // ["foo", index: 0, input: "foo"]
'foofoo'.match(/(?:foo){1,2}/) // ["foofoo", index: 0, input: "foofoo"]
'foofoo'.match(/foo{1,2}/) // ["foo", index: 0, input: "foofoo"]
复制代码
使用场景:示例表达式 /(?:foo){1,2}/
。若是表达式是 /foo{1,2}/
,{1,2}
将只对 foo
的最后一个字符 ’o‘
生效。若是使用非捕获括号,则 {1,2}
会匹配整个 foo
单词。
x(?=y)
, x(?!y)
, x|y
x(?=y)
:匹配'x'仅仅当'x'后面跟着'y';
x(?!y)
:匹配'x'仅仅当'x'后面不跟着'y';
x|y
: 匹配x或y;
这两种匹配的结果都不包含y。
例:
'JackSprat'.match(/Jack(?=Sprat)/) // ["Jack", index: 0, input: "JackSprat"]
'JackWprat'.match(/Jack(?=Sprat)/) // null
'JackWprat'.match(/Jack(?=Sprat|Wprat)/) // ["Jack", index: 0, input: "JackWprat"]
/\d+(?!\.)/.exec("3.141") // ["141", index: 2, input: "3.141"]
复制代码
{n}
, {n,m}
:
{n}
:匹配了前面一个字符恰好发生了n次;
{n,m}
:匹配前面的字符至少n次,最多m次。若是 n 或者 m 的值是0, 这个值被忽略;
例:
/a{2}/.exec('candy') // null
/a{2}/.exec('caandy') // ["aa", index: 1, input: "caandy"]
/a{2}/.exec('caaandy') // ["aa", index: 1, input: "caaandy"]
/a{1,3}/.exec('candy') // ["a", index: 1, input: "candy"]
/a{1,3}/.exec('caandy') // ["aa", index: 1, input: "caandy"]
/a{1,3}/.exec('caaandy') // ["aaa", index: 1, input: "caaandy"]
/a{1,3}/.exec('caaaandy') // ["aaa", index: 1, input: "caaaandy"]
复制代码
[xyz]
, [^xyz]
[xyz]
:一个字符集合。匹配方括号的中任意字符;
[^xyz]
:一个反向字符集。匹配任何没有包含在方括号中的字符;
这两种匹配均可以使用破折号(-)来指定一个字符范围,特殊符号在字符集中没有了特殊意义。
例:
function escapeRegExp(string){
return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$&");
//$&表示整个被匹配的字符串
}
复制代码
例子中的 .*+?^=!:${}()
都表示字面量,并无特殊意义
其余
\b
:匹配一个词的边界。一个匹配的词的边界并不包含在匹配的内容中。换句话说,一个匹配的词的边界的内容的长度是0;
\B
: 匹配一个非单词边界;
例:
/\bm/.exec('moon') // ["m", index: 0, input: "moon"]
/\bm/.exec('san moon') // ["m", index: 4, input: "san moon"]
/oo\b/.exec('moon') // null
/\B../.exec('noonday') // ["oo", index: 1, input: "noonday"]
/y\B../.exec('possibly yesterday') // /y\B../.exec('possibly yesterday')
复制代码
\d
:匹配一个数字,等价于 [0-9]
;
\D
:匹配一个非数字字符,等价于 [^0-9]
;
\f
:匹配一个换页符 (U+000C);
\n
:匹配一个换行符 (U+000A);
\r
:匹配一个回车符 (U+000D);
\s
:匹配一个空白字符,包括空格、制表符、换页符和换行符,等价于 [ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
;
\S
:匹配一个非空白字符,等价于 [^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
;
\w
:匹配一个单字字符(字母、数字或者下划线),等价于[A-Za-z0-9_]
;
\W
:匹配一个非单字字符,等价于[^A-Za-z0-9_]
;
g
:全局搜索;
i
:不区分大小写;
m
:多行搜索;
RegExp
有 exec()
和 test()
方法;
exec
匹配的结果为:匹配结果、捕获结果,index
和 input
。
test
匹配的结果为 true
或 false
,效率比 exec
要高。
String
有 match()
,replace()
,search()
,split()
方法;
match
匹配的结果同 RegExp
的 exec
,replace
根据正则表达式替换,search
查找因此位置,split
根据正则表达式分割字符串。
其中,当 replace
有 function
时,参数说明以下:
index
input
输入项本文只针对前端正则表达式的简单归纳,要用好表达式还须要具体的经验。