1

I have a string that contains multiple occurrences of ],[ that I want to replace with ]@[

No matter what I try I cant get it right.

    var find = '],[';
    var regex = new RegExp(find, "g");
    mytext.replace(regex, ']@[')

Doesn't work

   mytext = mytext.replace(/],[/g,']@[');

Doesn't work

Any idea where I am going wrong?

10
  • Please check out the following jsFiddle: jsfiddle.net/JspRR/3 to verify my answer. Commented Nov 29, 2012 at 15:21
  • The answer is that [ and ] are special characters in the context of regular expressions and as such need to be escaped either by means of \ i.e. to match ] you write [ Commented Nov 29, 2012 at 15:29
  • @MiltiadisKokkonidis It is working fine for me and replaces ALL occurrences.I didn't mark it accepted until I used his code and found it solved my issue. Commented Nov 30, 2012 at 8:16
  • Please have a look at the following jsFiddle: jsfiddle.net/JspRR/6 The version with the global regex does what you want, but the one with the string does not, right? Commented Nov 30, 2012 at 8:59
  • Also you may have a look at this: stackoverflow.com/questions/2992276/… Commented Nov 30, 2012 at 9:04

5 Answers 5

2

The answer is that [ and ] are special characters in the context of regular expressions and as such need to be escaped either by means of \ i.e. to match ] you write [ when you use the convenient Javascript shorthand for regular expressions that you can find in the code below:

var regex= /\],\[/g
var result = mytext.replace(regex, ']@[') 

Please check out the following jsFiddle: http://jsfiddle.net/JspRR/4/

As you can see the important bit is escaping the ] and the [ when constructing the regular expression.

Now if you did not want to use the Javascript regular expressions shorthand, you would still need to have the same escaping. However, in that case the \ character will need to be escaped itself ( ... by itself!)

var regex = new RegExp("\\],\\[", "g");
var result = mytext.replace(regex, ']@[') 
Sign up to request clarification or add additional context in comments.

Comments

1

The reason your example doesn't work is because normally square brackets represents a character class and therefore you need to escape them like so

var find = '\\],\\[';
var regex = new RegExp(find, "g");
mytext.replace(regex, ']@[')

You can also use a regex literal

mytext.replace(/\],\[/g, "]@[");

2 Comments

First answer that worked plus an explanation where I went wrong. Many thanks
Please see comments above and highlight the working version for anyone that views this accepted answer. Many thanks.
1

Try this:-

 mytext.replace(/\],\[/g, ']@[')

Comments

1

Square brackets are special characters inside a regular expression: they are used to define a character set.

If you want to match square brackets in a regexp, you have to escape them, using back slash.

"[1],[2],[3]".replace(/\],\[/g, "]@[");

Or, in case you use the builtin constructor:

"[1],[2],[3]".replace(new RegExp("\\],\\[", "g"), "]@[");

In both case we have to use the g flag so that the regular expression can match all the occurrences of the searched string.

var str = "[1],[2],[3]";

console.log(str.replace(/\],\[/g, "]@["));
console.log(str.replace(new RegExp("\\],\\[", "g"), "]@["));

1 Comment

Just correct regexp will be .replace(/\],\[/g,"]@[") (] and [ should be escaped)
0
var str = "[1],[2],[3]";

var replaced = str.replace('],[', ']@[');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.