REGEXP 对象
JS 通过内置对象 RegExp 支持正则表达式。
有两种方法实例化 RegExp 对象:字面量
、构造函数
。
-
字面量: var reg = /\bis\b/g
-
构造函数: var reg = new RegExp('\\bis\\b','g')
创建方法的区别
1在引擎编译代码时新建正则表达式,2在运行时新建正则表达式,所以1效率更高,而且1比较遍历和直观,在实际应用中基本上都使用1。
修饰符
g
:global 全文搜索,不添加,搜索到第一个匹配停止i
:ignore case 忽略大小写,默认大小写敏感m
:multiple lines 多行搜索u
:Unicode,将模式视为 Unicode 序列点的序列y
:粘性匹配,仅匹配目标字符串中此正则表达式的 lastindex 属性指示的索引s
: dotAll 模式,匹配任何字符(包括终止符 '\n')
正则表达式相关方法
方法 | 返回值 |
---|---|
RegExp.prototype.test(str) | true false |
RegExp.prototype.exec(str) | 结果数组 null |
String.prototype.search(reg) | index -1 |
String.prototype.match(reg) | 匹配数组 null |
String.prototype.split(reg) | 分割后数组 |
String.prototype.replace() | 替换后字符串 |
RegExp 对象
属性
- global
- ignoreCase
- Multiline
- lastIndex:下次匹配开始的字符串索引位置
- source:正则对象的源模式文本
方法
RegExp.prototype.test()
布尔值,表示当前模式能否匹配到字符串。
// 非全局调用
let reg = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','');
reg.test('2015-12-25');
// true
reg.test('2015-12-25');
// true
// 全局调用
let reg1 = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','g');
reg1.test('2015-12-25');
// true
reg1.test('2015-12-25');
// false
RegExp.prototype.exec()
数组或者 null,返回匹配结果。
- index:声明匹配文本的第一个字符的位置
- input:存放被检索的字符串 string
非全局调用
- 调用非全局的 RegExp 对象的 exec() 时,返回数组
- 第一个元素是与正则表达式项匹配的文本
- 第二个元素是与 RegExp 对象的第一个子表达式相匹配的文本(如果有的话)
- 第三个元素是与 RegExp 对象的第二个子表达式相匹配的文本(如果有的话,以此类推)
全局调用
时,对象是有状态的,会将上次匹配后的位置记录在 lastIndex 属性中。
// 非全局调用
let reg = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','');
let arr = reg.exec('2015-12-25');
// ["2015-12-25", "2015", "12", "25", index: 0, input: "2015-12-25", groups: undefined]
arr = reg.exec('2015-12-25');
// ["2015-12-25", "2015", "12", "25", index: 0, input: "2015-12-25", groups: undefined]
// 全局调用
let reg1 = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','g');
arr = reg1.exec('2015-12-25');
// ["2015-12-25", "2015", "12", "25", index: 0, input: "2015-12-25", groups: undefined]
arr = reg1.exec('2015-12-25');
// null
字符串对象
方法
String.prototype.search(reg)
- search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串
- 方法返回第一个匹配结果 index,查找不到返回 -1
- search() 方法不执行全局匹配,它将忽略标志 g,并且总是从字符串的开始进行检索
let reg1 = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','g');
i='2015-12-25'.search(reg1); //0
i='2015-12-25'.search(reg1); //0 忽略 g
String.prototype.match(reg)
- match() 方法将检索字符串,以找到一个或多个与 regexp 匹配的文本
- regexp 是否具有标志 g 对结果影响很大
非全局调用
(结果类似 exec() 方法)
- 如果 regexp 没有标志 g,那么 match() 方法只能在字符串中执行一次匹配
- 如果没有找到任何匹配的文本,将返回 null
- 否则将返回一个数组,其中存放了与它找到的匹配文本有关的信息
全局调用
- 如果 regexp 具有标志 g 则 match() 方法将执行全局检索,找到字符串中所有匹配子字符串
- 没有找到任何匹配的子串,返回 null
- 如果找到了一个或多个匹配子串,返回数组
- 数组元素中存放的是字符串中所有的匹配子串,没有 index 属性或 input 属性
// 非全局
let reg3 = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','');
'2015-12-25 2020-01-18'.match(reg3);
// ["2015-12-25", "2015", "12", "25", index: 0, input: "2015-12-25 2020-01-18", groups: undefined]
// 全局
let reg2 = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})','g');
'2015-12-25 2020-01-18'.match(reg2);
// ["2015-12-25", "2020-01-18"]
String.prototype.split()
可以传入正则,也是将参数默认转换为正则。
如果分隔符是包含捕获括号的正则表达式,则每次分隔符匹配时,捕获括号的结果(包括任何未定义的结果)将被拼接到输出数组中。但是,并不是所有浏览器都支持此功能。
let myString = "Hello 1 word. Sentence number 2.";
let splits = myString.split(/(\d)/);
// ["Hello ", "1", " word. Sentence number ", "2", "."]
String.prototype.replace()
- String.prototype.replace(str.replaceStr)
- String.prototype.replace(reg,replaceStr)
- String.prototype.replace(reg,function)
function 会在每次匹配替换的时候调用,有四个参数
- 匹配字符串
- 正则表达式分组内容,没有分组则没有该参数
- 匹配项在字符串中的 index
- 原字符串
'a1b2c3d4e5'.replace(/\d/g,function(match,index,origin){
console.log(index);
return parseInt(match)+1;
});
1
3
5
7
9
"a2b3c4d5e6"
'a1b2c3d4e5'.replace(/(\d)(\w)(\d)/g,function(match,group1,group2,group3,index,origin){
console.log(index,group1,group2,group3);
return group1+group2;
});
1 "1" "b" "2"
5 "3" "d" "4"
"a1bc3de5"