1. 新建正则表达式
-
使用直接量语法(/)
/正则表达式主体/修饰符(可选)
其中 ,修饰符是一个可选字符串,包括:
- i:执行大小写不敏感的匹配;
- g:执行全局匹配(查找所有匹配而非在查找到第一个后停止);
- m:执行多行匹配;
- u:使用“Unicode”编码进行匹配。 (es6新增)
- y:sticky(粘连)修饰符。类似g,但是要求下次搜索必须在上次搜索的头部匹配。ECMAScript6入门-正则表达式的扩展-y修饰符(阮一峰) (es6新增)
- s:dotAll模式,使得
.
可以匹配任何字符,包括换行符。ECMAScript6入门-正则表达式的扩展-s修饰符(阮一峰) (es6新增)
eg:
var regex = /wxc/i //i是一个修饰符,表示不区分大小写
正则表达式主题+修饰符等称为一个完整的正则表达式
-
使用RegExp构造函数
-
new RegExp(字符串[, 修饰符]);
var regex = new RegExp('wxc', 'i'); // 等价于 var regex = /wxc/i;
-
new RegExp(/正则表达式主体/修饰符(可选)
var regex = new RegExp(/wxc/i); // 等价于 var regex = /wxc/i;
-
new RegExp(/正则表达式主体/修饰符(可选), 第二个修饰符);
--es6新增es5不允许在第一个参数为正则表达式的情况下,使用第二个修饰符参数,es5会报错:
var regex = new RegExp(/wxc/, 'i'); // Uncaught TypeError: Cannot supply flags when constructing one RegExp from another
es6新增了这种写法,而且若第一个正则表达式参数含有修饰符,则会忽略原有修饰符并添加第二个参数的修饰符:
var regex = new RegExp(/wxc/ig, 'i'); console.log(regex.flags); //输出正则的修饰符--i
-
-
正则表达式符号:
-
\w
(查找单词字符)和\W
(查找非单词字符):单词字符包括:大小写字、数字和下划线。
-
\b
(匹配单词边界)和\B
(匹配非单词边界):匹配的只是位置,要匹配具体内容需要加其他内容。
eg:
var text = 'Hello World!'; console.log(text.match(/\bH/g)) //["H"] 查找以H开头的单词
-
\xxx
( 查找以八进制数 xxx 规定的字符)、\xdd
(查找以十六进制数 dd 规定的字符)查找规定的字符(默认是acill),对应进制的字符。
eg:
\127
匹配acill码的W(acill为87,8进制127)。 -
\uxxxx
( 查找以十六进制数 xxxx 规定的 Unicode 字符)同上,匹配的是Unicode
其他方括号、元字符、量词等的使用见JavaScript RegExp 对象-w3school
-
2. 字符串的正则方法
字符串对象共有 4 个方法,可以使用正则表达式:match()
、replace()
、search()
和split()
。
ES6 将这 4 个方法,在语言内部全部调用
RegExp
的实例方法,从而做到所有与正则相关的方法,全都定义在RegExp
对象上。
String.prototype.match
调用RegExp.prototype[Symbol.match]
String.prototype.replace
调用RegExp.prototype[Symbol.replace]
String.prototype.search
调用RegExp.prototype[Symbol.search]
String.prototype.split
调用RegExp.prototype[Symbol.split]
-
match()方法:
stringObject.match(regexp)
在字符串中检索指定的一个或多个正则表达式的匹配,并返回匹配到的值的数组。
eg:
var text = 'Hello World!'; var re1 = /l/; var re2 = /l/g; //全局匹配l console.log(text.match(re1)); //结果:["l", index: 2, input: "Hello World!", groups: undefined] console.log(text.match(re2)); // ["l", "l", "l"]
-
replace()方法:
stringObject.replace(regexp/substr,replacement)
替换与正则表达式匹配的子串。返回替换后的字符串,不会修改原有字符串。
eg:替换字符串中的world(不区分大小写)为China
var preText = 'Hello World!'; var re = /world/i; var chinaTest = preText.replace(re, 'China'); console.log(preText); //'Hello World!' console.log(chinaTest); //'Hello China!'
-
search()方法:
stringObject.search(regexp)
检索第一个与正则表达式相匹配的值。返回第一个匹配到的值的下标。
a t t e n t i o n \color{red}{attention} attention: 使用
search()
方法会忽略正则表达式中的g(全局修饰符),只会返回第一个匹配到的值的下标。eg:
var text = 'Hello World!'; var re1 = /l/; var re2 = /l/g; //全局匹配l console.log(text.search(re1)); //2 console.log(text.search(re2)); //2
-
split()方法:
stringObject.split(separator,howmany)
根据正则表达式将字符串分隔。如果设置了howmany参数,则数组长度不会超过howmany值。返回分隔后的数组。
a t t e n t i o n \color{red}{attention} attention: 与search()一样,g(全局修饰符)是没有意义的。
eg:
var text = 'Hello World!'; var re1 = /l/; var re2 = /l/g; //全局匹配l console.log(text.split(re1)); //["He", "", "o Wor", "d!"] console.log(text.split(re2)); //["He", "", "o Wor", "d!"]
3. RegExp对象的方法
RegExp对象共有 3 个方法:compile()
、exec()
和test()
。
-
compile()方法:
RegExpObject.compile(regexp,modifier)
编译正则表达式,或在执行过程中改变正则表达式。
看起来类似正则表达式的新建操作,其实可以提高正则表达式的执行速度,在大量使用正则时可以选择使用。
-
exec()方法:
RegExpObject.exec(string)
用于检测字符串中的正则表达式匹配值。若有匹配项,则返回一个数组,否则返回
null
。a t t e n t i o n \color{red}{attention} attention:
exec()
方法类似于stringObject.match()
,其返回结果有些时候看起来是一致的。-
当字正则对象不是全局匹配时,
exec()
与match()
的结果是一致的; -
当字正则对象不是全局匹配时 ,
match()
会返回字符串中的全部匹配项的数组,而exec()
返回的是从正则对象的lastIndex
属性后的额第一个匹配项,匹配后将lastIndex
赋值为匹配到的下标+1( 当 匹 配 到 最 后 一 个 之 后 , 再 次 匹 配 会 返 回 n u l l , 并 且 l a s t I n d e x 归 0 \color{red}{当匹配到最后一个之后,再次匹配会返回null,并且lastIndex归0} 当匹配到最后一个之后,再次匹配会返回null,并且lastIndex归0)。所以可以反复调用exec()
来遍历字符串中的所有匹配项。
eg:
- 非全局匹配:
exec()
与match()
对比
var text = 'Hello World!'; var re1 = /l/; console.log(text.match(re1)); //结果:["l", index: 2, input: "Hello World!", groups: undefined] consolr.log(re1.exec(text)); //["l", index: 2, input: "Hello World!", groups: undefined]
- 全局匹配:
exec()
与match()
对比
var text = 'Hello World!'; var re2 = /l/g; //全局匹配l var result; console.log(text.match(re2)); // ["l", "l", "l"] while((result=re2.exec(text)) != null){ console.log(re2.lastIndex); //输出当前的lastIndex console.log(result); } /* while循环输出结果: 3 ["l", index: 2, input: "Hello World!", groups: undefined] 4 ["l", index: 3, input: "Hello World!", groups: undefined] 10 ["l", index: 9, input: "Hello World!", groups: undefined] */ //匹配到最后,result为null时,lastIndex归0 console.log(re2.lastIndex); //0
-
-
test()方法:
RegExpObject.test(string)
检测一个字符串是否匹配某个正则表达式。如果有,则返回
true
,否则返回false
。eg:
var str = 'Hello World!'; var re1 = /world/; var re1 = /world/i; //不区分大小写 console.log(re1.test(str)); //false console.log(re2.test(str)); //true