0

I have the following string:

var foo = "{(y-7)}({x + d})"
var find = "{(y-7)}";
var replacement = "12";
var re = new RegExp(find, 'g');
foo = foo.replace(re, replacement);

But this results in the exact same string, without any changes. But, if I remove the parens i.e "(" and ")" from the expression, then it seems to work. Why

Why won't it match when the expressions contain "("?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

2 Answers2

4

You have to escape ( and ) since they have special meaning in regex:

var find = "\\{\\(y-7\\)\\}"

Same stands for { and }.

This escaping only needs to happen in regexes (i.e. your find), foo is fine as is since that's just an ordinary string.

I suggest you use JavaScript's built in functionality to have less escaping:

var re = /\{\(y-7\)\}/g;

Update

The string is handed to me like that, so I need to do a replace

var find = "{(y-7)}";
find = find.replace("\\", "\\\\")
           .replace("(", "\\(").replace(")", "\\)")
           .replace("{", "\\{").replace("}", "\\}");

I call this "escaping hell", but generating a regex is not trivial, though you may get lucky. I suggest you learn to use it first and then try to generate it.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • My strings "find" etc are created programmatically. How can I escape the "{" and "(" then. Can you explain ? – Rahul Iyer Oct 25 '14 at 13:14
  • If even the `find` is generated, then you must have a code like this: `find = "(" + somevar + ")"`, escape (put backslash before) the `(){}` there. – TWiStErRob Oct 25 '14 at 13:16
  • I can't do that. The string is handed to me like that, so I need to do a replace. – Rahul Iyer Oct 25 '14 at 13:18
  • curly brackets are indeed special characters but most of the time (when they are not around an integer or two integers separated by comma) you don't need to escape them. – Casimir et Hippolyte Oct 25 '14 at 13:18
  • @CasimiretHippolyte better safe than sorry :) – TWiStErRob Oct 25 '14 at 13:18
  • I still don't understand how to escape the "{" and "(" if they are already in the string. It seems like a recursive problem. If I new how to escape it, then I could escape it! XD – Rahul Iyer Oct 25 '14 at 13:20
  • 1
    @John in other languages, it could be possible through `\Q\E` block. . But js won't support this feature.. – Avinash Raj Oct 25 '14 at 13:21
4

Characters that have a special meaning in a regular expression needs to be escaped. You escape them by putting a backslash in front of them, and to put a backslash in a string you need to escape that by putting a backslash in front of it:

var find = "\\{\\(y-7\\)\\}";

(In some situations characters doesn't need escaping in a regular expression, because it can be understood without it, but start by escaping all characters that have a special meaning, then you can read up on the exact situations where it is not needed.)

Demo:

var foo = "{(y-7)}({x + d})"
var find = "\\{\\(y-7\\)\\}";
var replacement = "12";
var re = new RegExp(find, 'g');
foo = foo.replace(re, replacement);

// show result in StackOverflow snippet
document.write(foo);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • "find" is created programmatically. How can I escape the "{" then ? – Rahul Iyer Oct 25 '14 at 13:16
  • 1
    @John: You can escape the characters using `find = find.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");`. Found here: http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript/494122#494122 – Guffa Oct 25 '14 at 13:20