如何向现有正则表达式添加标志?

发布于 2025-01-08 13:09:50 字数 113 浏览 2 评论 0原文

我有一堆正则表达式,例如 /[az]/。稍后在我的程序中,我需要将其设置为 /[az]/g, 所以我需要稍后添加“全局”修饰符。如何向现有正则表达式添加修饰符?

I have a bunch of regular expression, e.g. /[a-z]/. Later in my program I need to have this as /[a-z]/g,
so I need to add the 'global' modifier later. How can I add a modifier to an existing regular expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淑女气质 2025-01-15 13:09:50

使用正则表达式 flags 用于将正则表达式与标志分开。然后使用该字符串创建一个新字符串并设置所需的标志。

var re = /^[a-z]*$/;
var re2 = new RegExp(re.source, re.flags + "i");

console.log( re.test("abc") )
console.log( re.test("ABC") )
console.log( re2.test("abc") )
console.log( re2.test("ABC") )

Use RegEx source and flags to separate the regular expression from the flags. Then create a new one with the string and set the needed flags.

var re = /^[a-z]*$/;
var re2 = new RegExp(re.source, re.flags + "i");

console.log( re.test("abc") )
console.log( re.test("ABC") )
console.log( re2.test("abc") )
console.log( re2.test("ABC") )
只为一人 2025-01-15 13:09:50

你可以为它写一个方法 -

RegExp.prototype.reflag= function(flags){
    return RegExp(this.source, flags);
}

You can write a method for it-

RegExp.prototype.reflag= function(flags){
    return RegExp(this.source, flags);
}
吾性傲以野 2025-01-15 13:09:50

这是一个基于 epascarello 的答案和评论的函数。你说你有很多正则表达式需要稍后修改,你可以重新定义它们引用的变量,或者通过函数调用创建一些新的变量。

function modifyRegexpFlags(old, mod) {
    var newSrc = old.source;
    mod = mod || "";
    if (!mod) {
        mod += (old.global) ? "g" : "";
        mod += (old.ignoreCase) ? "i" : "";
        mod += (old.multiline) ? "m" : "";
    }
    return new RegExp(newSrc, mod);
}

var lower = /[a-z]/;
//Some code in-between
lower = modifyRegexpFlags(lower, "g");

如果省略第二个参数,则将使用旧的修饰符。
(Credit to davin for the idea).

Here is a function to build on epascarello's answer and the comments. You said you have quite a few of regexps to modify later on, you could just redefine the variable they are referenced in or make some new ones with a function call.

function modifyRegexpFlags(old, mod) {
    var newSrc = old.source;
    mod = mod || "";
    if (!mod) {
        mod += (old.global) ? "g" : "";
        mod += (old.ignoreCase) ? "i" : "";
        mod += (old.multiline) ? "m" : "";
    }
    return new RegExp(newSrc, mod);
}

var lower = /[a-z]/;
//Some code in-between
lower = modifyRegexpFlags(lower, "g");

If the second argument is omitted, the old modifiers will be used.
(Credit to davin for the idea).

关于从前 2025-01-15 13:09:50

早期的答案中没有真正涵盖一个方面,所以添加我的两分钱......

这里的优秀答案(为 epascarello +1!)并没有完全涵盖所有基础。如果您想概括该函数以允许将任何标志添加到任何正则表达式中:

function addregexflags(regx, newflags) {
    // add new flags without duplication (won't work in old browsers or IE)
    newflags = [...new Set([...regx.flags.split(''), ...newflags.split('')])].join('');
    return new RegExp(regx.source, newflags);
}

addregexflags(/try/gi, "gm");    // /try/gim

如果您必须支持不支持 Sets 和展开运算符的旧版浏览器,则需要手动合并字符串,因为 RegExp 构造函数不允许复制的旗帜。

One aspect not really covered in earlier answers, so adding my two cents...

The excellent answers (+1 for epascarello!) here don't quite cover all the bases. If you want to generalize the function to allow any flags added to any regex:

function addregexflags(regx, newflags) {
    // add new flags without duplication (won't work in old browsers or IE)
    newflags = [...new Set([...regx.flags.split(''), ...newflags.split('')])].join('');
    return new RegExp(regx.source, newflags);
}

addregexflags(/try/gi, "gm");    // /try/gim

If you have to support older browsers that don't support Sets and the spread operator, you need to longhand the union of strings as the RegExp constructor does not allow replication of flags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文