一、正则的创建
正则表达式:字符串的校验(验证) — 字符串
- 字面量创建 /匹配的字符串/
const reg1 = /good/ ;
- 实例化对象 new RegExp(‘匹配的字符串’)
RegExp是regular expression 的简写
const reg2 = new RegExp('good') ;
二、正则的修饰符
- i ignore 忽略大小写
const reg1 = /tmd/i ;
- g global 全局匹配
const reg2 = /tmd/g ;
用法:构造函数的第一个参数表示匹配的字符,第二个参数表示修饰符
const reg3 = new RegExp('tmd' , 'gi') ;
replace的用法:replace 默认只会替换第一个
const reg3 = new RegExp('tmd' , 'gi') ;
const str = 'hello tmd hei Tmd tmd' ;
// replace 默认只会替换第一个
console.log(str.replace('tmd' , '**'));
//hello ** hei Tmd tmd
console.log(str.replace(/tmd/ig , '**'));
//hello ** hei ** **
console.log(str.replace(reg3 , '**'));
//hello ** hei ** **
三、正则的方法
- test() 返回布尔值:测试字符串中是否有匹配正则的内容, 一般此正则不需要全局匹配
const reg = /tmd/ig ;
const str = 'hello tmd t] tMd' ;
console.log(reg.test(str)); //true
- exec() 返回的是一个数组 — 总是匹配一个 , 如果匹配不到就返回null
const reg = /tmd/ig ;
const str = 'hello tmd t] tMd' ;
console.log(reg.exec(str));
三、正则的语法和元字符
正则的语法
- | 或者
-
[ ] 或者(区间) [0-9] [1-9] [a-z] [A-Z] [0-9a-zA-Z]
- 空格也会被匹配 — 一般正则不写空格
const reg = /[0-9]/ ;
const reg2 = /0|1|2|3|4|5|6|7|8|9/ ;
const reg3 = /[0-9,; ]/
console.log(reg3.test('你, 好啊')); //true
正则的元字符
元字符 | 匹配次数 |
---|---|
* | 0次或者多次 {0 , } |
? | 0次或者1次 {0 , 1} |
+ | 1次或者多次 {1 , } |
{m , n} | m次到n次 |
{m , } | 至少m次 |
{m} | m次 |
^ | 以…开头 |
$ | 以…结尾 |
. | 匹配任意字符 |
注意:^ $ 同时使用可以把正则规定死
下面来看几个简单的例子吧:
const reg4 = /da*y/ ;
// d要么有a,要么没有a,不能有其他的字符y/
const str4 = 'dbaaaaaaaaaaaay dby up' ;
console.log(reg4.test(str4)); //false
const reg5 = /da+y/ ; //day daay daa...y
console.log(reg5.test('dy')); //false
const reg6 = /da?y/ ; dy day
console.log(reg6.test('day')); //true
const reg7 = /da{2,5}y/ ; // a匹配2次到5次
console.log(reg7.test('day')); //false
常见的正则验证:
1.验证手机号
2.验证数字开头
3.验证数字字母下划线组成
4.验证为空
5.用户名 3-6位 数字字母下划线组成,数字不能开头
// 验证手机号
const reg1 = /^1[0-9]{10}$/ ;
console.log(reg1.test('a18888888888'));
//用户名 3-6位
const reg2 = /^.{3,6}$/ ;
//数字开头
const reg3 = /^[0-9]/ ;
//数字字母下划线组成
const reg4 = /^[0-9a-zA-Z_]+$/ ;
// 为空
const reg5 = /^.{0}$/ ;
//用户名 3-6位 数字字母下划线组成,数字不能开头
const reg6 = /^[a-zA-Z_][0-9a-zA-Z_]{2,5}$/ ;
四、转义字符
转义使用 \
- 匹配 /
const reg = /\\\// ;
- 匹配 .
const reg2 = /\./ ;
- 匹配 [
const reg3 = /\[/ ;
+ ? * {} 匹配的次数,需要前面有匹配的东西
const reg4 = /\+/ ; console.log(reg4.test('1+1'));
五、正则的简写
\d | 数字 |
\D | 非数字 |
\w | 数字,字母,下划线 [0-9a-zA-Z_] |
\W | 非数字,字母,下划线 |
\s | 匹配空白字符 |
\S | 匹配非空白字符 |
注意:空白字符和空字符不一样,空字符 .{0}
- **正则的中文匹配:[\u4e00-\u9fa5] **
//匹配3-6个中文
const reg = /^[\u4e00-\u9fa5]{3,6}$/ ;
console.log(reg.test('你好啊啊啊'));
五、字符串使用到的正则方法
- replace(要查找的,替换的) 默认只会替换一次,因此需要正则
const mgc = ['tmd' , 'md' , 'wc'] ;
const str = mgc.join('|') ;
const reg = new RegExp(str , 'ig') ;
const word = 'day day ig' ;
console.log(word.replace(/day/g , '***'));
// *** *** ig
这里我们封装一个函数:替换敏感词。
//str要替换的字符串语句
//arr存储敏感词的数组
//n敏感词的替换样式
function replaceMinGan(str , arr , n){
//短路赋值
n = n || '***' ;
const word = arr.join('|') ;
const reg = new RegExp(word , 'ig') ;
return str.replace(reg , n) ;
}
const mgc = ['tmd' , 'md' , 'wc'] ;
const res = replaceMinGan('wc , 正则真tmd简单啊 , Wc' , mgc) ;
console.log(res); //*** , 正则真***简单啊 , ***
- search() 一般和indexOf是一样的,但是search还可以接受正则。search()方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,并返回子串的起始位置。
const str2 = 'good daay Daay up' ;
console.log(str2.search('day')); // 找不到返回-1
console.log(str2.search('up')); // 找到返回字符串起始位置 15
//search可以使用正则
console.log(str2.search(/da*y/gi)); //找到返回字符串起始位置5
- match() 找到满足条件的字符,默认只会找到第一个,全局匹配才能找到所有的,如果找不到就返回null
const str2 = 'good daay Daay up AA' ;
//找到满足条件的字符,默认找第一个
console.log(str2.match('a'));
//全局匹配下找到所有
console.log(str2.match(/a/gi));
- split() 把字符串切割成数组,也可以接受正则的写法
const str3 = 'good good study day day up' ;
//按照一个或者多个空格切割
console.log(str3.split(/ +/));
六、常见的正则练习
- 邮箱 123@qq.com 123@qq.cn 123@qq.com.cn。必须有@ , @前后必须有内容, .com或者 .cn 或者 .com .cn结尾。(@前的内容可以是字母数字下划线,\ @后内容可以是数字字母下划线 后面加上 .com 或 .cn 或 .com 或 .cn)
const reg = /^[\w,\+]+@\w+\.(com|cn|com\.cn)$/
console.log(reg.test('12\@qq.com')); //true
- 身份证号:6位 + 4位出生年份 + 2月 + 2日期 + 3个数字 + 数字或者X
const reg3 = /^\d{6}(19|20)\d{2}(0|1)\d(0|1|2|3)\d\d{3}[\dX]$/ ;
console.log(reg3.test('364757200412243450')); //true
- 删除所有的空格
const str = ' good good study day day up ';
console.log(str.replace(/ +/g , ''));
//goodgoodstudydaydayup
- 删除首尾空格
const str = ' good good study day day up ';
console.log(str.replace(/^ +| +$/g , ''));
//good good study day day up
- 删除多余的空格
const str = ' good good study day day up ';
console.log(str.replace(/ +/g , ' '));
// good good study day day up