Js中RegExp对象

RegExp对象表示正则表达式,是由普通字符和特殊字符也叫元字符或限定符组成的文字模板,用于对字符串执行模式匹配。

描述

创建一个RegExp对象通常有两种方式,一种是通过字面量创建,一种是通过RegExp对象构造函数创建。

// var regex = /pattern/modifiers;
var regex = /^[0-9]+$/g;

// var regex = new RegExp("pattern", "modifiers");
var regex = new RegExp("^[0-9]+$", "g");

其中模式pattern描述了表达式的模式,修饰符modifiers用于指定全局匹配、区分大小写的匹配和多行匹配等等。

RegExp.prototype.compile()

regexObj.compile(pattern, flags)
compile()方法被用于在脚本执行过程中重新编译正则表达式,但是该特性已经从Web标准中删除,不推荐compile()方法,可以使用RegExp构造函数来得到相同效果。

var regex = /^[0-9]+$/g;
regex = regex.compile("^[0-9]+$", "i");
console.log(regex); // /^[0-9]+$/i

RegExp.prototype.exec()

regexObj.exec(str)
exec()方法在一个指定字符串中执行一个搜索匹配,返回一个结果数组或null,在设置了globalsticky标志位的情况下,RegExp对象是有状态的,其会将上次成功匹配后的位置记录在lastIndex属性中,使用此特性exec()可用来对单个字符串中的多次匹配结果进行逐条的遍历包括捕获到的匹配,而相比之下String.prototype.match()只会返回匹配到的结果。

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = regex.exec("2020-09-02");
console.log(res); // ["2020-09-02", "2020", "09", "02", index: 0, input: "2020-09-02", ... ]

// 进行一次完整的全局正则匹配需要使用RegExp.prototype.exec()或String.prototype.matchAll()
// 因为当使用String.prototype.match()和/g标志方式获取匹配信息时,捕获组会被忽略。
const regMatch = (regex, str) => {
    var result = [];
    var temp = null;
    var flags = `${regex.flags}${regex.flags.includes("g") ? "" : "g"}`; // 必须加入g修饰符 否则会陷入死循环
    regex = new RegExp(regex, flags);
    while (temp = regex.exec(str)) result.push(temp);
    return result;
}

RegExp.prototype.test()

regexObj.test(str)
test()方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配,返回truefalse

var regex = /^[0-9]+$/g;
console.log(regex.test("1")); // true

str.search(regexp)
search()方法执行正则表达式和String对象之间的一个搜索匹配,如果传入一个非正则表达式对象regexp,则会使用new RegExp(regexp)隐式地将其转换为正则表达式对象,如果匹配成功,则search()返回正则表达式在字符串中首次匹配项的索引,否则返回-1

var regex = /[0-9]+/g;
console.log("s123".search(regex)); // 1

String.prototype.match()

str.match(regexp)
match()方法检索返回一个字符串匹配正则表达式的结果,如果传入一个非正则表达式对象,则会隐式地使用new RegExp(obj)将其转换为一个RegExp,如果没有给出任何参数并直接使用match()方法 ,将会得到一个包含空字符串的Array[""],如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组,如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组Array

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = "2020-09-02".match(regex);
console.log(res); // ["2020-09-02"]

String.prototype.matchAll()

str.matchAll(regexp)
matchAll()方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器,如果传入一个非正则表达式对象,则会隐式地使用new RegExp(obj)将其转换为一个RegExp,传入的RegExp必须是设置了全局模式g的形式,否则会抛出异常TypeError,返回一个迭代器,不可重用,结果耗尽需要再次调用方法,获取一个新的迭代器。matchAll内部做了一个regexp的复制,所以不像regexp.exec,lastIndex在字符串扫描时不会改变。

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = "2020-09-02".matchAll(regex);
console.log([...res]); // 使用Spread操作符展开 也可以调用next()方法进行迭代
// [["2020-09-02", "2020", "09", "02", index: 0, input: "2020-09-02", ... ]]

String.prototype.replace()

str.replace(regexp|substr, newSubStr|function)
replace()方法返回一个由替换值replacement替换部分或所有的模式pattern匹配项后的新字符串,模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数,如果pattern是字符串,则仅替换第一个匹配项,原字符串不会改变。

var regex = /\d+/g;
var res = "s1s11s111".replace(regex, "");
console.log(res); // sss

String.prototype.split()

str.split([separator[, limit]])
split()方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置,separator指定表示每个拆分应发生的点的字符串,separator可以是一个字符串或正则表达式,limit提供一个整数,限定返回的分割片段数量,返回源字符串以分隔符出现位置分隔而成的一个Array

var regex = /\d+/g; // 以数字分割
var res = "2020-09-02".split(regex);
console.log(res); // ["", "-", "-", ""]

属性

方法

RegExp.prototype.compile()

regexObj.compile(pattern, flags)
compile()方法被用于在脚本执行过程中重新编译正则表达式,但是该特性已经从Web标准中删除,不推荐compile()方法,可以使用RegExp构造函数来得到相同效果。

var regex = /^[0-9]+$/g;
regex = regex.compile("^[0-9]+$", "i");
console.log(regex); // /^[0-9]+$/i

RegExp.prototype.exec()

regexObj.exec(str)
exec()方法在一个指定字符串中执行一个搜索匹配,返回一个结果数组或null,在设置了globalsticky标志位的情况下,RegExp对象是有状态的,其会将上次成功匹配后的位置记录在lastIndex属性中,使用此特性exec()可用来对单个字符串中的多次匹配结果进行逐条的遍历包括捕获到的匹配,而相比之下String.prototype.match()只会返回匹配到的结果。

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = regex.exec("2020-09-02");
console.log(res); // ["2020-09-02", "2020", "09", "02", index: 0, input: "2020-09-02", ... ]

// 进行一次完整的全局正则匹配需要使用RegExp.prototype.exec()或String.prototype.matchAll()
// 因为当使用String.prototype.match()和/g标志方式获取匹配信息时,捕获组会被忽略。
const regMatch = (regex, str) => {
    var result = [];
    var temp = null;
    var flags = `${regex.flags}${regex.flags.includes("g") ? "" : "g"}`; // 必须加入g修饰符 否则会陷入死循环
    regex = new RegExp(regex, flags);
    while (temp = regex.exec(str)) result.push(temp);
    return result;
}

RegExp.prototype.test()

regexObj.test(str)
test()方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配,返回truefalse

var regex = /^[0-9]+$/g;
console.log(regex.test("1")); // true

RegExp.prototype[@@match]()

regexp[Symbol.match](str)
对正则表达式匹配字符串时,[@@match]()方法用于获取匹配结果,这个方法的使用方式和String.prototype.match()相同,不同之处是this和参数顺序。

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = regex[Symbol.match]("2020-09-02");
console.log(res); // ["2020-09-02"]

RegExp.prototype[@@matchAll]()

regexp[Symbol.matchAll](str)
[@@matchAll]方法返回对字符串使用正则表达式的所有匹配项,这个方法的使用方式和String.prototype.matchAll()相同,不同之处是this和参数顺序。

var regex = /(\d{4})-(\d{2})-(\d{2})/g;
var res = regex[Symbol.matchAll]("2020-09-02");
console.log([...res]); // // [["2020-09-02", "2020", "09", "02", index: 0, input: "2020-09-02", ... ]]

RegExp.prototype[@@replace]()

regexp[Symbol.replace](str, newSubStr|function)
[@@replace]()方法会在一个字符串中用给定的替换器,替换所有符合正则模式的匹配项,并返回替换后的新字符串结果,用来替换的参数可以是一个字符串或是一个针对每次匹配的回调函数,这个方法基本可以和String.prototype.replace()一样使用,不同之处是this和参数顺序。

var regex = /\d+/g;
var res = regex[Symbol.replace]("s1s11s111", "");
console.log(res); // sss

regexp[Symbol.search](str)
[@@search]()方法执行了一个在给定字符串中的一个搜索以取得匹配正则模式的项,这个方法的使用方式和String.prototype.search()相同,不同之处是this和参数顺序。

var regex = /\d+/g;
var res = regex[Symbol.search]("s1s11s111");
console.log(res); // 1

RegExp.prototype[@@split]()

[@@split]()方法切割String对象为一个其子字符串的数组,这个方法的使用方式和 String.prototype.split()相同,不同之处是this和参数顺序。

var regex = /\d+/g;
var res = regex[Symbol.split]("2020-09-02");
console.log(res); // ["", "-", "-", ""]

RegExp.prototype.toString()

regexObj.toString()
toString() 返回一个表示该正则表达式的字符串。

var regex = /\d+/g;
console.log(regex.toString()); // /\d+/g

正则规则

元字符的规则列表以及它们在正则表达式上下文中的行为,该部分出自https://www.runoob.com/regexp/regexp-metachar.html

示例

该部分出自https://c.runoob.com/front-end/854

校验数字的表达式

校验字符的表达式

特殊需求表达式

参考

https://c.runoob.com/front-end/854
https://www.jianshu.com/p/7dbf4a1e6805
https://juejin.im/post/6844903816781889543
https://www.runoob.com/regexp/regexp-metachar.html
https://www.cnblogs.com/y896926473/articles/6366222.html
https://www.cnblogs.com/kevin-yuan/archive/2012/09/25/2702167.html
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp