正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等
1.正则表达式的创建
字面量模式
在一对反斜线中写正则表达式内容,如/abc/
正则表达式里面不需要加引号 不管是数字型还是字符串型
构造函数模式
构造正则表达式的实例,如new RexExp('abc')
内部传入的参数为字符串/字符串的变量
var str = 'hello world';
//字面量创建
var reg = /hello/g;
console.log(reg.exec(str)); //[ 'hello', index: 0, input: 'hello world', groups: undefined ]
//构造函数创建
var reg1 = new RegExp('world','img');
console.log(reg1.exec(str)); //[ 'world', index: 6, input: 'hello world', groups: undefined ]
2.字符分类
普通字符
字母、数字、下划线、汉字、没有特殊含义的符号(,;!@等)
实际上不是特殊字符的字符都是普通字符
特殊字符
\:将特殊字符转义成普通字符
模式修饰符
i:ignoreCase,匹配时忽视大小写
m:multiline,多行匹配
g:global,全局匹配
字面量创建正则时,模式修饰符写在一对反斜线后
3.正则表达式实例方法
exec
可用来匹配字符串中符合正则表达式的字符串
如果匹配到,返回值是一个result数组,否则返回null
[匹配的内容,index: 在str中匹配的起始位置,input: 参数字符串,groups: undefined]
var str = 'hello world hello kitty';
var reg1 = /hello/;
var reg2 = /hello/g;
var reg3 = /yyds/g;
console.log(reg1.exec(str)); //[ 'hello',index: 0,input: 'hello world hello kitty',groups: undefined ]
console.log(reg2.exec(str)); //[ 'hello',index: 0,input: 'hello world hello kitty',groups: undefined ]
console.log(reg3.exec(str)); //null
// 如果是全局模式的正则验证 还可以使用循环进行输出
while(true) {
var res = reg2.exec(str);
if(!res) {
break;
}
console.log(res[0],res['index'],reg2.lastIndex); //hello 12 17
}
test
用来测试待检测的字符串中是否有可以匹配到正则表达式的字符串,如果有返回true,否则返回false
var str = 'hello world hello kitty';
var reg1 = /world/;
var reg2 = /hi/;
console.log(reg1.test(str)); //true
console.log(reg2.test(str)); //false
注意:
- 如果正则表达式中有修饰符"g",这时,在正则表达式的实例reg中会维护lastIndex属性,记录下一次开始的位置,当第二次执行exec的时候,从lastIndex开始检索。
- 如果正则表达式中没有修饰符"g",不会维护lastIndex属性,每次执行从开始位置检索
toString/toLocaleString
把正则表达式的内容转化成字面量形式字符串/有本地特色的字符串(JS中没效果)
valueOf
返回正则表达式本身
var str = 'hello world hello kitty';
var reg1 = /hello/;
console.log(reg1.toString()); //返回/hello/字符串
console.log(reg1.toLocaleString()); //返回/hello/字符串
console.log(reg1.valueOf()); // 返回正则表达式本身 /hello/
4.正则表达式实例属性
lastIndex
当没设置全局匹配时,该属性值始终为0
设置了全局匹配时,每执行一次exec/test来匹配,lastIndex就会移向匹配到的字符串的下一个位置,当指向的位置后没有可以再次匹配的字符串时,下一次执行exec返回null,test执行返回false,然后lastIndex归零,从字符串的开头重新匹配一轮
可以理解成,每次正则查找的起点就是lastIndex
var str = 'hello world hello kitty hello boy';
var reg1 = /hello/;
var reg2 = /hello/g;
console.log(reg1.lastIndex); //0
console.log(reg1.exec(str)); //返回第一个hello
console.log(reg1.lastIndex); //0
console.log(reg2.lastIndex); //0
console.log(reg2.exec(str)); //返回第一个hello
console.log(reg2.lastIndex); //5
console.log(reg2.lastIndex); //5
console.log(reg2.exec(str)); //返回第二个hello
console.log(reg2.lastIndex); //17
console.log(reg2.lastIndex); //17
console.log(reg2.exec(str)); //返回第三个hello
console.log(reg2.lastIndex); //29
console.log(reg2.exec(str)); //null
console.log(reg2.lastIndex); //0
console.log(reg2.exec(str)); //返回第一个hello
ignoreCase、global、multiline
判断正则表达式中是否有忽略大小写、全局匹配、多行匹配三个模式修饰符
var reg1 = /hello/igm;
console.log(reg1.ignoreCase); //true
console.log(reg1.global); //true
console.log(reg1.multiline); //true
source
返回字面量形式的正则表达式(类似于toString)
var reg1 = /hello/igm;
console.log(reg1.source); //hello