前言
此文围绕javaScript中的正则表达式进行整理,用于记录javaScript中正则的使用
初始化
-
字面量初始化
// 匹配单词is const regExpText = /\bis\b/g // /\bis\b/g -
对象实例化
// 匹配单词is const regExpText = new RegExp('\\bis\\b', 'g') // /\bis\b/g
其他类型转正则
new RegExp(null) // /null/
new RegExp(undefined) // /(?:)/
new RegExp(123) // /123/
new RegExp(false) // /false/
new RegExp('hello') // /hello/
new RegExp({a:123}) // /[object Object]/
new RegExp([1,2,3]) // /1,2,3/
正则对象中的方法
RegExp.prototype.exec()
描述
在制定字符串中执行一个搜索匹配,当搜索匹配时,返回单元素数组,如果正则使用“/g”或者“/y”标志同时在RegExp对象的lastIndex属性中记录位置,下次使用此正则对象匹配字符时,从lastIndex位置开始匹配,如果未匹配,返回null,laseIndex置空,如果正则没使用“/g”或者“/y”则laseIndex永远为0
语法
regexObj.exec([str])
参数
- str:字符串,可不传,不传返回null
返回值
- 如果匹配成功,
exec()方法返回一个数组(包含额外的属性index和input等)并更新正则表达式对象的lastIndex属性 - 如果匹配失败,
exec()方法返回null,lastIndex属性置为0
例子
var regExp1 = /.at/
var regExp2 = /.at/g
var str1 = 'lat yat gat'
var str2 = 'lat nnn mmm'
regExp1.exec(str1) // ["lat", index: 0, input: "lat yat gat", groups: undefined]
regExp1.lastIndex // 0
regExp2.exec(str1) // ["lat", index: 0, input: "lat yat gat", groups: undefined]
regExp2.lastIndex // 3
regExp2.exec(str2) // null
regExp2.lastIndex // 0
RegExp.prototype.test()
描述
执行一个检索,用来查看郑泽表达式与指定的字符串是否匹配。返回true或false
语法
regexObj.test([str])
参数
- str:字符串,可不传,不传返回false
返回值
- 如果正则表达式与指定的字符串匹配 ,返回
true;否则false - 如果正则携带/g标志,返回true时设置lastIndex,返回false时置空lastIndex
例子
var regexObj = /.at/g
regexObj.test('lat gat vat nat') // true
regexObj.lastIndex // 3
regexObj.test() // false
regexObj.lastIndex // 0
字符串中关于正则的方法
String.prototype.match()
描述
检索返回一个字符串匹配正则表达式的结果,不修改原字符串
语法
str.match([regexp])
参数
- regexp:如果传入非正则表达式对象,则会隐式转换为正则对象,如果为空,则默认传入undefined,然后按转换规则转换为正则对象
返回值
- 如果使用g标志,则返回与完整正则表达式匹配的所有结果,但不会返回捕获组
- 如果未使用g标志,则仅返回第一个完整匹配及相关的捕获组(Array)。同时会返回附加属性(groups、input、input)
- 如果未找到返回null
- 如果参数为空,则以
/(?:)/作为正则对象,返回空字符串Array"[""]",同时会返回附加属性
例子
var str = 'hello world'
str.match() // ["", index: 0, input: "hello world", groups: undefined]
str.match('你好') // null
str.mathc('o') // ["o", index: 4, input: "hello world", groups: undefined]
str.match(/o/g) // ["o", "o"]
str.match(/o/) // ["o", index: 4, input: "hello world", groups: undefined]
String.prototype.matchAll()
描述
检索返回一个字符串匹配正则表达式的结果,同时以迭代器的形式返回,不修改原字符串
语法
str.matchAll([regexp])
参数
- regexp:如果传入非正则表达式对象,则会隐式转换为正则对象,如果为空,则默认传入undefined,然后按转换规则转换为正则对象
返回值
- 不管是否使用g标志,都将与完整正则表达式匹配的所有结果存于正则迭代器中返回
- 如果未找到,返回空的正则迭代器
- 如果参数为空,则返回以
/(?:)/作为正则对象,挨个匹配后的正则迭代器
例子
var str = 'h'
var result = str.matchAll()
result.next() // {value: ["", index: 0, input: "h", groups: undefined], done: false}
result.next() // {value: ["", index: 0, input: "h", groups: undefined], done: false}
result.next() // {value: undefined, done: true}
var str = 'hello world'
var result = str.matchAll('o')
result.next() // {value: ["o", index: 4, input: "hello world", groups: undefined], done: false}
result.next() // {value: ["o", index: 7, input: "hello world", groups: undefined], done: false}
result.next() // {value: undefined, done: true}
var str = 'hello world'
var result = str.matchAll(/o/)
result.next() // {value: ["o", index: 4, input: "hello world", groups: undefined], done: false}
result.next() // {value: ["o", index: 7, input: "hello world", groups: undefined], done: false}
result.next() // {value: undefined, done: true}
var str = 'hello world'
var result = str.matchAll(/o/g)
result.next() // {value: ["o", index: 4, input: "hello world", groups: undefined], done: false}
result.next() // {value: ["o", index: 7, input: "hello world", groups: undefined], done: false}
result.next() // {value: undefined, done: true}
var str = 'hello world'
var result = str.matchAll('你好')
result.next() // {value: undefined, done: true}
String.prototype.search()
描述
搜索字符串中符合正则规则的子字符串,返回子字符串的开始索引,索引从零开始计数
语法
str.search([regexp])
参数
- regexp:如果传入非正则表达式对象,则会隐式转换为正则对象,如果为空,则默认传入undefined,然后按转换规则转换为正则对象
返回值
- 如果匹配成功,返回对应的索引
- 如果匹配失败,返回-1
例子
var str = 'hello world'
str.search() // 0
str.search('o') // 4
str.search(/o/) // 4
str.search(/o/g) // 4
String.prototype.replace()
描述
根据正则规则或者字符串检索原字符串,用新字符串或函数返回值替换检索到的字符,返回新的字符串,不修改原字符串
语法
str.replace([regexp|substr, newSubStr|function])
参数
- regexp: 一个正则对象或其字面量
- subser:任意参数,如果是undefined则被转换为“undefined”字符串
- newSubStr:新字符串,用于替换匹配部分的字符串
- function:用来创建新子字符串,该函数的返回值将替换掉第一个参数匹配的结果
返回值
- 新的字符串
例子
var str = 'hello world'
str.replace('o', '你好') // "hell你好 world"
str.replace(/o/g, '你好') // "hell你好 w你好rld"
str.replace(/(o)/g, '你好($1)') // "hell你好(o) w你好(o)rld"
str.replace(/he(ll)o/g, (param1, param2) => {
console.log(param1)
console.log(param2)
return '你好'
})
// hello
// ll
// "你好 world"
String.prototype.split()
描述
使用指定的分隔符字符串将一个原字符串分割成子字符串数组,并返回数组,不修改原字符串
语法
str.split([separator[, limit]])
参数
- separator:表示拆分应发生的点的字符串,可以是字符串或者正则表达式
- limit:限制字符串长度,如果裁剪后的总长度超过limit,则截取0-limit
返回值
- 字符串数组
注意
不建议使用str.split('')拆分字符串,针对特殊字符串容易拆分出错
例子
var str = '你们undefined你好'
str.split('undefined') // ['你们', '你好']
str.split(/undefined/) // ['你们', '你好']
str.split() // ['你们undefined你好']
'𝟘𝟙𝟚𝟛'.split('') // [ '�', '�', '�', '�', '�', '�', '�', '�' ]