# 正则学习记录

[toc]

# 一、正则表达式的创建

# 1. 字面量创建

1
const a = /a/igm //i表示忽略大小写,g表示全局匹配,m表示多行匹配

# 2. 构造函数创建

1
2
3
cosnt a = 'a'
const b = new RegExp('a') 或者 b = new RegExp(a)//这里的b打印结果与上面字面量创建的a一致
// RegExp有两个参数,第一个参数传入一个字符串表示正则表达式,第二个参数有g全局匹配,i忽略大小写,m多行匹配

# 二、JavaScript 中正则方法

# 1. test 方法

用于测试某一字符串是否满足某一正则表达式,返回一个布尔值。正则表达式的方法

1
2
3
const reg = /a/
const str = 'abc'
reg.test(str)

# 2. exec 方法

用于匹配某一字符串中满足正则表达式的内容,返回一个数组,第一项为匹配到的内容,第二项 index 为匹配到内容第一个字符所在的索引,input 为匹配的字符串,groups 为命名的捕获组。正则表达式的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const reg = /a/
const str = 'abcabcaa'
const result = reg.exec(str)
console.log(result) //[ 'a', index: 0, input: 'abcabcaa', groups: undefined ]
/*
如果正则表达式是全局匹配的话,exec会改变的lastindex,如果exec没匹配上,这时lastindexhi变为0
*/
const reg = /a/g
const str = 'abac'
for(let i=0;i<3;i++){
console.log(reg.lastIndex)
reg.exec(str)
console.log(reg.lastIndex)
}// 0,1,1,3,3,0
//可以用?<name>给捕获组命名。这时如果成功匹配返回的对象中groups不再是undefined而是一个属性为捕获组名字,值为匹配到的内容的对象。
const reg = /(?<year>[0-9]+)-(?<month>[0-9]+)-(?<day>[0-9]{2})/
const str = '2023-07-13'
const result = reg.exec(str)
console.log(result)
/*
[
'2023-07-13',
'2023',
'07',
'13',
index: 0,
input: '2023-07-13',
groups: [Object: null prototype] { year: '2023', month: '07', day: '13' }
]
*/
const reg = /(?<year>[0-9]+)-(?<month>[0-9]+)-(?<day>[0-9]{2})/g
const str = '2023-07-13 1145-14-15'
reg.exec(str).groups
const {year,month,day} = reg.exec(str).groups
console.log(year,month,day) // 1145 14 15

# 3. match 方法

匹配字符串中满足正则表达式的内容并返回一个数组,注意 match 是字符串的方法不是正则表达式的方法。

1
2
3
4
5
6
7
const reg = /a/
const regG = /a/g
const str = 'abcabcaa'
const result = str.match(reg) // 如果正则表达式不是全局匹配这时match返回值与exec一致
console.log(result)//[ 'a', index: 0, input: 'abcabcaa', groups: undefined ]
const resultG = str.match(regG)// 这时会返回一个数组,数组中每一项为满足字符串中满足正则表达式中匹配规则的部分。
console.log(resultG)//[ 'a', 'a', 'a', 'a' ]

# 4. search 方法

匹配字符串中满足正则表达式的内容并返回第一个匹配到字符的索引。同样这也是字符串方法

1
2
3
4
5
6
7
const reg = /a/
const regG = /a/g
const str = 'babcabcaa'
const result = str.search(reg)
const resultG = str.search(regG)// 全局匹配一样只返回匹配到第一个满足条件的字符的索引
console.log(result)//1
console.log(resultG)//1

# 5. replace () 方法

匹配字符串中满足某字符串或者正则表达式的内容并进行替换,同样也是字符串方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const reg = /a/
const regG = /a/g
const str = 'babcabcaa'
const result = str.replace(reg,'*') //没有全局匹配只匹配第一个满足条件的
const resultG = str.replace(regG,'*')
console.log(result) //b*bcabcaa
console.log(resultG) //b*bc*bc**
/*
replace的第二个参数可以传递一个函数,函数的参数为匹配到的内容,如果正则表达式是全局匹配的话且能匹配到多个字符,则每匹配到一个就会调用一次函数,形参为匹配到的内容
*/
const result = str.replace(reg,(data)=>{
console.log(data) // a
})
const resultG = str.replace(regG,(data)=>{
console.log(data) // a
}) //回调函数调用了3次

# 6. split 方法

用字符串分割数组,分割方式可以为一个正则表达式并返回一个数组,数组每一项为分割后的字符串。

1
2
3
4
5
6
7
8
9
10
11
12
const str = 'abc'
const newstr = str.split('')
console.log(newstr) //[ 'a', 'b', 'c' ]
/*
JavaScript中字符串的方法有限,如果想要反转字符串,简单的办法是用split('')转化为数 组然后调用reverse方法反转数组并调用join('')转回字符串
*/
const reStr = str.split('').reverse().join('')
console.log(reStr) // cba

const str = '2023-7-12' //如果想要这个字符串中的数字
const newstr = str.split(/-/)
console.log(newstr) [ '2023', '7', '12' ]