替换javascript中所有带大括号的字符串

替换javascript中所有带大括号的字符串,javascript,Javascript,我想说: i went to the [open shops], but the [open shops] were closed 像这样: i went to the markets, but the markets were closed 用javascript替换 我对正则表达式不是很在行,方括号需要分隔,我确定试试这个: "i went to the [open shops], but the [open shops] were closed".replace(/\[open sho

我想说:

i went to the [open shops], but the [open shops] were closed
像这样:

i went to the markets, but the markets were closed
用javascript替换

我对正则表达式不是很在行,方括号需要分隔,我确定

试试这个:

"i went to the [open shops], but the [open shops] were closed".replace(/\[open shops\]/g, 'markets');
棘手的部分是需要跳出括号并添加全局匹配以替换每个匹配实例。有关更多信息:

请尝试以下操作:

"i went to the [open shops], but the [open shops] were closed".replace(/\[open shops\]/g, 'markets');

棘手的部分是需要跳出括号并添加全局匹配以替换每个匹配实例。有关更多信息:

如果您不想使用正则表达式。你可以用类似的东西

    var a = "i went to the [open shops], but the [open shops] were closed";
    var replacement = "KAPOW!";

    while(a.contains("[") && a.contains("]"))
    {
        var left = a.indexOf("[");
        var right = a.indexOf("]");

        a = a.substring(0,left) + replacement + a.substring(right+ 1);
    }

    console.log(a);

如果你不想使用正则表达式。你可以用类似的东西

    var a = "i went to the [open shops], but the [open shops] were closed";
    var replacement = "KAPOW!";

    while(a.contains("[") && a.contains("]"))
    {
        var left = a.indexOf("[");
        var right = a.indexOf("]");

        a = a.substring(0,left) + replacement + a.substring(right+ 1);
    }

    console.log(a);

您只需将\置于[和]之前,将其视为常规字符。这样,您的正则表达式将成为
\[openshops\]

如果需要替换多个对象(例如
[shops]
[state]
),可以执行以下操作,动态创建正则表达式。这样你就不必为每件事都硬编码

var str = "I went to the [shops], but the [shops] were [state]. I hate it when the [shops] are [state].";
    var things = {
        shops: "markets",
        state: "closed"
    };
    for (thing in things) {
        var re = new RegExp("\\["+thing+"\\]", "g");
        str = str.replace(re, things[thing]);
    }
console.log(str);

请注意,这样做时需要使用两个反斜杠,而不是一个。

您需要做的是将\放在[和]之前,将其视为常规字符。这样,您的正则表达式将成为
\[openshops\]

如果需要替换多个对象(例如
[shops]
[state]
),可以执行以下操作,动态创建正则表达式。这样你就不必为每件事都硬编码

var str = "I went to the [shops], but the [shops] were [state]. I hate it when the [shops] are [state].";
    var things = {
        shops: "markets",
        state: "closed"
    };
    for (thing in things) {
        var re = new RegExp("\\["+thing+"\\]", "g");
        str = str.replace(re, things[thing]);
    }
console.log(str);

请注意,这样做时,您需要使用两个反斜杠,而不仅仅是一个。

也许正则表达式教程或在线正则表达式测试员会是更好的第一步?@DaveNewton:或者甚至是谷歌,也许正则表达式教程或在线正则表达式测试员会是更好的第一步?@DaveNewton:甚至谷歌,谢谢-我很接近-我在整个正则表达式上加了引号。替换(“/[openshops]/g”,“sd”)。我想空间可能也需要逃离。但显然不是。再次感谢。@case1352在一个正则表达式问题中,包含您正在使用的正则表达式可能是有意义的。谢谢-我很接近-我在整个正则表达式上加了引号。替换(“/[open shops]/g”,“sd”)。我想空间可能也需要逃离。但显然不是。再次感谢。@case1352在一个正则表达式问题中,包含您正在使用的正则表达式可能是有意义的。不过,在这种情况下,正则表达式似乎是一个更好的工具。如果不是的话,阅读一个简单的正则表达式要比阅读几行代码容易得多。这是正则表达式知识的不足,我同意正则表达式是更好的选择,但是对于初学者来说,就像刚刚做的hello world tuts的技能水平一样,我认为正则表达式是复杂性向高水平的飞跃。但这可能只是我,我只是提供了一个我觉得更容易阅读的选择。首先,我可能会以不同的方式构建字符串:不过,在这种情况下,D.Regex似乎是一个更好的工具。如果不是因为缺少Regex知识,阅读简单的Regex要比阅读几行代码容易得多。我同意Regex是更好的选择,但对于初学者程序员来说,就像刚刚做的hello world tuts的技能水平一样,我认为regex是复杂性向高水平的跳跃。但这可能只是我,我只是提供了一个我觉得更容易阅读的选择。首先,我可能会以不同的方式构建字符串:D。