正则表达式

基础知识

元字符

转义字符 说明
\w 匹配0-9, a-z, A-Z, _字符
\W 匹配非0-9, a-z, A-Z, _字符
\d 匹配0-9的数字
\D 匹配非0-9的数字
\s 匹配空格, tab-\t、换行符\n
\S 匹配非空格, tab-\t、换行符\n
  • 匹配任意字符:\s\S

特殊字符

特殊字符 说明
\ 转义字符
. 表示任意字符,但不包括换行
| 表示或
[] 表示匹配方括号中的任意一个字符
[^] 表示不能匹配方括号中的任意一个字符

定位字符

定位字符 说明
^ 匹配字符串开始位置
$ 匹配字符串结束位置
\b 单词边界
\B 非单词边界
  • 单词边界:我们知道单词是以空格隔开的,那么单词的边界就是一个单词的开始或结尾处
let str = "tea act aet";
let reg1= /\ba/; 
let reg2 = /\Ba/;
console.log(str.match(reg1)); //['a', index: 4, input: 'tea act aet', groups: undefined]
console.log(str.match(reg2)); //['a', index: 2, input: 'tea act aet', groups: undefined]

限定符:表示重复次数

限定符 说明
+ 表示 + 前面的内容,重复 1次 到 多次
* 表示 * 前面的内容,重复 0次 到 多次
? 表示 ? 前面的内容,重复 0次 到 1次
表示 {} 前面的内容,重复 x次
表示 {} 前面的内容,重复 x次 到 y次
表示 {} 前面的内容,重复 x次 到 多次
  • 正向预查\向前查找:(?=n)
    匹配指定字符 n 之前的内容

  • 非向前查找: (?!n)

let str ="abaaa";
let reg =/a(?=b)/g;
let reg1 =/a(?!b)/g;
console.log(str.match(reg)); //[a]
console.log(str.match(reg1)); //['a', 'a', 'a']
  • 贪婪匹配和非贪婪匹配
    正则表达式所有量词默认都是贪婪匹配的。
    要变成非贪婪模式---->只需在量词后加 ?
let str = "aaaa";
let reg1 = /a+/g;
let reg2 = /a+?/g;
//贪婪模式
console.log(str.match(reg1)); //['aaaa']
//非贪婪模式
console.log(str.match(reg2)); //['a', 'a', 'a', 'a']

regExp相关方法

  • 方法:reg.test(str)
    str字符串中是否包含符合reg规则的片段,符合返回true,不符合返回false,主要用于验证输入是否正确。
let reg = /^\d{3}-\d{8}$/;
let phone = "023-12345678"
console.log(reg.test(phone)); //true
  • 方法:reg.exec(str)
  • exec方法需要配合g 下标才会变化
  • 属性:reg.lastIndex
    lastIndex 配合exec方法用
let str = "aaabbccc";
let reg = /(\w)\1/g; 
console.log(reg.exec(str)); // ['aa', 'a', index: 0, input: 'aaabbccc', groups: undefined]
console.log(reg.lastIndex); // 2
console.log(reg.exec(str)); // ['bb', 'b', index: 3, input: 'aaabbccc', groups: undefined]
console.log(reg.lastIndex); // 5
console.log(reg.exec(str)); // ['cc', 'c', index: 5, input: 'aaabbccc', groups: undefined]
console.log(reg.lastIndex); // 7
console.log(reg.exec(str)); // null
console.log(reg.lastIndex); // 0
console.log(reg.exec(str)); // ['aa', 'a', index: 0, input: 'aaabbccc', groups: undefined]

说明:()内的是子表达式, 1表示反向引用第一个表达式匹配出来的内容 2表示反向引用第二个表达式匹配出来的内容, 子表达式中的内容会作为返回值返回到数组里面

字符串能使用正则的方法

  • string.match(reg)
    加了g:返回匹配到的所有字符数组
    不加g:返回匹配到的第一个类数组
    没有匹配到返回null
let str = "aabbccdd";
let reg = /(\w)\1(\w)\2/g; 
console.log(str.match(reg)); //['aabb', 'ccdd']
  • string.search()
    返回匹配到的第一个字符串位置,不支持g, 没有匹配到返回-1。
let str = "12345"
let reg1 = /3/  
let reg2 = /6/  
console.log(str.search(reg1)) //2
console.log(str.search(reg2)) //-1

-string.replace(string|RegExp, string|function)
第一个参数式匹配内容,第二个参数式需要替换的内容。
注意:当第一个参数是字符串时,只能替换第一个匹配的内容,如果要全局替换,需要使用全局模式的正则表达式。

let str = "aa";
console.log(str.replace("a", "b")); //ba

let str2 = "1122";
let reg2 = /(\d)\1(\d)\2/g
console.log(str2.replace(reg2, "$2$2$1$1")); //2211
console.log(str2.replace(reg2, function($,$1,$2){
  //第一个参数式匹配的结果,第二、第三都是子表达式内容
  return $2+$2+$1+$1;
})); //2211

//将字符串变成小驼峰写法
let str3 = "the-first-name";
let reg3 = /-(\w)/g;
console.log(str3.replace(reg3, function($, $1){
  return $1.toUpperCase();
})) //theFirstName
  • string.split(reg)
    注意:如果正则有子表达式的情况。
    不同浏览器存在差异。
let str = "aaa6ffff0sss";
let reg1 = /\d/;
let reg2 = /(\d)/;
console.log(str.split(reg1)); //['aaa', 'ffff', 'sss']
console.log(str.split(reg2)); //['aaa', '6', 'ffff', '0', 'sss']

习题练习

  • 1、将给定字符串去重, aaabbcccc 变成 abc
let str = "aaabbcccc";
let reg = /(\w)\1*/g;
console.log(str.replace(reg, "$1")); //abc
  • 2、将给出的数字每3位用逗号隔开,100000000 变成 100,000,000
let str = "100000000";
let reg = /\B(?=(\d{3})+$)/g
console.log(str.replace(reg, ".")); //100.000.000
posted @ 2022-12-04 13:26  smile_or  阅读(41)  评论(0编辑  收藏  举报