1

i want to make this:

i went to the [open shops], but the [open shops] were closed

look like this:

i went to the markets, but the markets were closed

with javascript replace

im not very good with regex and the square brackets need delimited im sure

case1352
  • 1,142
  • 1
  • 13
  • 22
  • 1
    Maybe a regex tutorial or an online regex tester would be a better first step? – Dave Newton Jun 09 '15 at 01:46
  • @DaveNewton: Or even google for that matter http://stackoverflow.com/questions/4292468/javascript-regex-remove-text-between-brackets – frenchie Jun 09 '15 at 02:05

3 Answers3

3

Try this:

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

The tricky part is the the need to escape the brackets and add the global match to replace each matching instance. For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

akosel
  • 1,133
  • 9
  • 14
  • thanks - i was close - i was putting quotes around the whole regex thus .replace("/\[open shops\]/g","sd"). i thought the space might need escaping too. but obviously not. thanks again. – case1352 Jun 09 '15 at 02:16
  • @case1352 In a regex question it might make sense to include the regex you're using. – Dave Newton Jun 09 '15 at 21:56
1

All you need to do is put \ before [ and ] to treat it as a regular character. This way your regex would become \[openshops\].

If you have multiple things that need to be replaced (eg. [shops] and [state]) you can do the following which dynamically creates the regex. This way you don't have to hard code it for each thing.

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);

Note that you need to use two backslashes instead of just one when doing it this way.

3ocene
  • 1,833
  • 1
  • 14
  • 29
0

If you don't want to use regex. You could use something like.

    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);
Spaceman
  • 1,181
  • 11
  • 38
  • 1
    Regex seems like a better tool in this case, though. – Dave Newton Jun 09 '15 at 21:57
  • 1
    It's a lot easier to read a simplistic regex than several lines of code-if it isn't it's a deficiency in regex knowledge, which IMO is critical to have. – Dave Newton Jun 09 '15 at 23:58
  • I agree regex is the better option but for beginner programmers, like just did hello world tuts level of skill, I think regex is a jump of complexity to high. But that is probably just me I was just offering an alternative that I find easier to read. I would probably just build the string differently in the first place :D. – Spaceman Jun 10 '15 at 00:50