0

I need to replace the text between two parentheses using Regex in Javascript. For example:

var x = "I need to go (now)";

I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:

x.replace(/\(now)\b/g, 'tomorrow');
hindmost
  • 7,125
  • 3
  • 27
  • 39
netgen
  • 3
  • 1

1 Answers1

3
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');

You don't need the \b and you need to escape the second ).

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Mike
  • 4,071
  • 21
  • 36
  • Does that replace all occurrences? – netgen Aug 06 '15 at 15:29
  • The code in my post will replace all occurrences because of the `g` flag at the end of the regex. Remove it to only replace the first occurrence. – Mike Aug 06 '15 at 15:30