例子
正则表达式例子
1、日期 xxxx-yy-zz xxxx/yy/zz
^(\d{4})[/-](\d{2})[/-](\d{2})$
$1 $2 $3
2、邮箱 xxxxxx@ssdsdsds.xcc
^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+.[a-zA-Z]{2,4}$
3、电话 第一位1,第二位3~9,后续是3~11数字
^1[3-9][0-9]{9}$
^1[3-9]\d{9}$
4、巧用replace
'abc12345#$*%'.match(/([^\d]*)(\d*)([^\w]*)/)
//replace
function replacer(match, p1, p2, p3, index, origin) {
return [p1, p2, p3].join(' - ');
}
var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
定义
匹配字符串,正则表达式对象
这些模式被用于 RegExp
的 exec
和 test
方法, 以及 String
的 match
、matchAll
、replace
、search
和 split
方法
一、正则表达式字面量(/*/)
var re = /ab+c/;
二、正则表达式对象
var reg = new RegExp('\\d')
正则组成
断言(向前向后表达式)
断言的组成之一是边界
- ^(始)、$(终)、\b(单词边界)、\B(非单词边界)
- 其他断言 ?
- x(?=y)向后断言 /jack(?=chari)/ 匹配jack
- x(?!y)向前否定断言 var reg = /\d+(?!.)/ ; reg.exec(3.34) ; 匹配34
- (?<=y)x向后断言 /(?<=jack|Tom)Spart/.exec(‘TomSpart’)
- (?<!y)x向后否定断言/(?<!jack|tom)Spart/.exec(‘jackSpart’)
字符
原字符(字母、数字)
元字符(* + ? $ ^ . | \ () {} [])
转译\元字符字符类[a,b,c] oneOf(a,b,c)
[]
取反[^]'abceee'.replace(/[abc]/g,'l')
'abceee'.replace(/[^abc]/g,'l')
范围类[a-z][a-zA_Z]可以联写
开头结尾代表范围,有头没尾,有尾没头代表字符-
在类里面匹配[-]'2020-11-10'.replace(/[0-9-]/g,'l')
预定义类
.\d [0-9]
\D [^0-9]
\w[a-zA_Z0-9]
\W[^a-zA-Z0-9] …
边界字符
\b \B ^ $
组合范围
可选flag
格式:var reg = /pattern/flags
重点 ⚠️ ⚠️ ⚠️ 标示g和y的情况,可以设置lastIndex,其他lastIndex无效
g |
全局搜索。 |
---|---|
i |
不区分大小写搜索。 |
m |
多行搜索。 |
s |
允许 . 匹配换行符。 |
u |
使用unicode码的模式进行匹配。 |
y |
执行“粘性(sticky )”搜索,匹配从目标字符串的当前位置开始。 |
var reg = /\d/,reg2 = /\d/y,reg3=/\d/g
reg 不支持lastIndex
reg2 支持,匹配方法和reg一样
reg3 支持
量词
匹配字符和表达式的范围
? {0,1}
+ {1,}
* {0,} 任意次
{n}
{n,m}
{n,}
贪婪模式
{m,n} 条件满足按照n匹配
'12345678'.replace(/\d{3,6}/g,'x') 'x78'
非贪婪模式
{m,n}?
'12345678'.replace(/\d{3,6}?/g,'x') 'xx78'
分组
量词分组
或分组
(jack|jerry)Win
bric(ks|sk)wong
反向引用
$1,$2,… 捕获内容
'2020-11-10'.replace(/(\d{4})-(\d{2})-(\d{2})/g,'$2-$3-$1')
忽略分组
?: 分组但是不捕获内容
'2020-11-10'.replace(/(?:\d{4})-(\d{2})-(\d{2})/g,'$1-$2')
regExp属性
global (默认false),ignoreCase(默认false) , multiline (默认false)
lastIndex:当前表达式匹配内容的最后一个字符下一个位置 ,当做下一次匹配的开始
source:正则表达式文本字符串
属性列表
lastIndex
dotAll /\w/s bool 允许.匹配换行符 bool
sticky /\w/y 粘性搜索 bool
unicode /\w/u 使用unicode码匹配 bool
flags /\w/gimusy 返回值 gimusy
regExp方法
注意/g 的 返回值index 和 reg.lastIndex变化
- exec匹配到 返回数组[匹配的字符,()存储的变量,groups【返回undefined】,index,input]
- test 返回值bool
compile
constructor
toString
字符串方法
[exec](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)(正则对象) |
一个在字符串中执行查找匹配的RegExp方法,它返回一个数组(未匹配到则返回 null)。 | |
---|---|---|
[test](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) (正则对象) |
一个在字符串中测试是否匹配的RegExp方法,它返回 true 或 false。 | |
[match](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/match) match(regExp) |
一个在字符串中执行查找匹配的String方法,它返回一个数组,在未匹配到时会返回 null。 | |
[matchAll](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) match(regExp) |
一个在字符串中执行查找所有匹配的String方法,它返回一个迭代器(iterator)。必须是//g标示 | |
[search](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/search) search(regExp) 非正则参数,search将参数转换成正则 |
一个在字符串中测试匹配的String方法,它返回匹配到的位置索引,或者在失败时返回-1。忽略g标识,总是从0开始 | |
`replace replace(regExp,newval | function)` | 一个在字符串中执行查找匹配的String方法,并且使用替换字符串替换掉匹配到的子字符串。 |
`split split(“separator | regExp”,limit)` | 一个使用正则表达式或者一个固定字符串分隔一个字符串,并将分隔后的子字符串存储到数组中的 String 方法。 |
案例
字符串是否存在某个模式
regExp.test()返回bool
string.search()返回索引位置
string.macth()、regExp.exec()运行会慢一些(返回值是数组或者null)
string.replace(regExp,newStr|function)
replace
注意第二参数是函数,函数的返回值
如果是g,函数是每次匹配到就会执行
'a1b2c3'.replace(/\d/g,function(macth,index,origin){
return Number(macth)+1
})
match和matchAll
一、⚠️⚠️⚠️ 正则表达式不同(/g)返回值不同,/i不受影响
'test1test2'.match(/^test/i)
["test", index: 0, input: "test1test2", groups: undefined]
'test1test2'.match(/^test/g)
["test"]
matchAll必须是//g、 返回值是RegExp String Iterator、匹配到是[[],[]],匹配不到是[]
'asdasdasd2asdasd3'.matchAll(/\d/g)
RegExpStringIterator(RegExp String Iterator) 格式类似[[],[]]
展开
[...'asdasdasd2asdasd3'.matchAll(/\d/g)]
资源
MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions
粘性标示y https://zh.javascript.info/regexp-sticky
工具
图形正则 https://regexper.com/
快捷字符自查表 https://tool.oschina.net/uploads/apidocs/jquery/regexp.html