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,026
  • 3
  • 25
  • 37
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
  • 155,085
  • 27
  • 166
  • 222
Mike
  • 4,051
  • 20
  • 35
  • 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