RegExp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let reg = /[\u4e00-\u9fa5]/ console.dir(reg)
|
RegExp.prototype.dotAll 在正则表达式中是否使用 s
修饰符,s
修饰符可以匹配任意单个字符。只读属性,属于单个正则表达式实例。
***暂留,我承认我没有搞明白***
RegExp.prototype.flags 返回由当前正则表达式对象的标志组成的字符串,是一个只读属性
1 2 3 4 5 6 7 8 9 10 11 12 13
| let reg = /[\u4e00-\u9fa5]/sg console.dir(reg)
|
RegExp.prototype.global 正则表达式是否使用了 g
标志,是找到所有匹配,而不是第一个匹配后停止,是一个只读属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let reg = /[gimuy]*$/g console.dir(reg)
|
RegExp.prototype.ignoreCase 正则表达式是否使用了 i
标志,是否忽略大小写,只读属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let reg = /[\u4e00-\u9faf]/gi console.dir(reg)
|
RegExp.prototype.multiline 正则表达式中使用 m
属性,多行匹配,只读属性
“m” 标志意味着一个多行输入字符串被看作多行
使用 “m”,”^” 和 “$” 将会从只匹配正则字符串的开头或结尾,变为匹配字符串中任一行的开头或结尾。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let reg = /[\u4e00-\u9faf]/gim console.dir(reg)
|
RegExp.prototype.exec() 在一个指定字符串中执行一个搜索匹配。返回匹配数组或 null。
regexObj.exec(str)
*** notice ***
1 2 3
| reg = /[\u4e00-\u9fa5]/ reg.exec('1233 我是张丽萍 abc')
|
索引 |
值 |
描述 |
0 |
我 |
匹配的全部字符串 |
1 |
index:5 |
匹配字符串开始索引 |
2 |
1233 我是张丽萍 abc |
匹配原始字符串 |
3 |
groups:undefined |
分组捕获的结果 |
加全局匹配标识符,会记录上次匹配的位置,可以循环遍历。注意不要在遍历条件中写入正则表达式,如果一直匹配,会造成死循环
。
1 2 3 4 5 6 7 8 9 10 11
| const reg = /(\w+)(\s)/g const str = 'abc abc abc' reg.exec(str)
|
返回值 []
- [0] 匹配的字符串([‘abc ‘])
- [1],…[n] 分组捕获([‘abc’, ‘ ‘])
- index 匹配到的字符位于原始字符串的基于0的索引值 (index: 0)
- input 原始字符串
字符集合(character sets)
[xyz] [x-z] 匹配是 xyz 的字符集
1 2 3 4
| /[x-z]/.exec('zxyd')
/[^x-z]/g.exec('xyzabdxyz')
|
[^xyz] [^x-z] 匹配非 xyz 的字符集
分组(grouping)
1 2 3 4
| regGroups = /(\d+)([\u4e00-\u9fa5]+)(\w)/ regGroups.exec('1233我是张丽萍abc')
|
索引 |
值 |
描述 |
0 |
1233我是张丽萍a |
匹配的全部字符串 |
1 |
1233 |
分组匹配的结果 RegExp.$1 |
2 |
我是张丽萍 |
分组匹配的结果 RegExp.$2 |
3 |
a |
分组匹配的结果 RegExp.$3 |
4 |
index |
匹配到的字符位于原始字符串的基于0的索引值 |
5 |
groups:undefined |
分组捕获的结果 |
字符类别(Character Classes)
字符 |
含义 |
\d [0-9] |
匹配任意阿拉伯字符串 |
边界(Boundaries)
字符 |
含义 |
^ |
匹配输入开始 位置,如果设置 flag multiline,该标识会匹配每个断行(line-break)符后的开始 位置 |
$ |
|