正则(RegExp):用于检测字符串是否符合该规则,符合返回值为true,不符合返回值为false
const reg=/a/ // 字符串中含有a即可
const reg0=new RegExp('a')
const reg1_1=/\d/
const reg1_2=/[0-9]/
const reg1_3=/\D/
const reg2_1=/\w/
const reg2_2=/\W/
const reg3_1=/\s/
const reg3_2=/\S/
匹配 数字或字母
const reg4=/[0-9a-zA-Z]/
console.log(reg3_1.test('1sd')); // false
console.log(reg3_1.test('asd ')); // true
console.log(reg4.test('1sd')); // true
console.log(reg4.test('1sd '));// true
console.log(reg4.test('_ ')); // false
const reg5=/^js/ // 以js开头
console.log(reg5.test('jsqwe')); // true
const reg6=/js$/ // 以js结尾
console.log(reg6.test('123js')); // true
const reg7=/^js$/
console.log(reg7.test('jsjs')); // false
console.log(reg7.test('js')); // true
const reg1_1=/z*/
const reg1_2=/^z*$/
console.log(reg1_1.test('zzz')); // true
console.log(reg1_1.test('zzzdd')); // true
console.log(reg1_2.test('zzz')); // true
console.log(reg1_2.test('zzzdd')); // false
const reg2_1=/z+/
const reg2_2=/^z+$/
console.log(reg2_1.test('')); // false
console.log(reg2_1.test('zzzd')); // true
console.log(reg2_2.test('')); // false
console.log(reg2_2.test('zzz')); // true
console.log(reg2_2.test('zzzdd')); // false
const reg3=/^(zx)?$/
console.log(reg3.test('')); // true
console.log(reg3.test('zx')); // true
console.log(reg3.test('zxzx'));// false
const reg4=/^(zx){2}$/
console.log(reg4.test('zx')); // false
console.log(reg4.test('zxzx')); // true
console.log(reg4.test('zxzxzx')); // false
const reg5=/^(zx){2,}$/
console.log(reg5.test('zx')); // false
console.log(reg5.test('zxzx')); // true
console.log(reg5.test('zxzxzx')); // true
const reg6=/^(zx){2,4}$/
console.log(reg6.test('zxzx')); // true
console.log(reg6.test('zxzxzxzx')); // true
console.log(reg6.test('zxzxzxzxzx')); // false
const reg1_1=/^[xyz]$/
const reg1_2=/[xyz]/
console.log(reg1_1.test('xx')); // false
console.log(reg1_1.test('x')); // true
console.log(reg1_1.test('p')); // false
console.log(reg1_2.test('xx')); // true
console.log(reg1_2.test('x')); // true
console.log(reg1_2.test('p')); //false
const reg2=/^[0-9a-zA-Z_]$/
console.log(reg2.test('33')); //false
console.log(reg2.test('3')); //true
console.log(reg2.test('_')); //true
const reg4=/^[^xyz]$/
console.log(reg4.test('xx')); // false
console.log(reg4.test('x')); // false
console.log(reg4.test('p')); // true
let str='今年是tmd个好日子,tmd,学好js,tmd,TMD,tMd'
// 替换敏感词
// replace
const newStr=str.replace('tmd','**')
console.log(newStr)
console.log(str);
let newStr1=str.replace(/tmd/,'**')
console.log(newStr1);
let newStr2=str.replace(/tmd/g,'**')
console.log(newStr2); //今年是**个好日子,**,学好js,**,TMD,tMd
let newStr3=str.replace(/tmd/gi,'**')
console.log(newStr3); // 今年是**个好日子,**,学好js,**,**,**