-5

Sorry, this sounds very basic, but I really can't find on Google.

In order to replace contents in a string globally, you can use things like...

 a.replace(/blue/g,'red')

But sometimes you need to replace characters that's not compatible with the example above, for example, the character ")"

So, this will fail...

const a ="Test(123)"
a = a.replace(/(/g, '')

VM326:1 Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group

How to replace string of characters like that ? at :1:7

Ori Drori
  • 166,183
  • 27
  • 198
  • 186
Marco Jr
  • 5,844
  • 9
  • 38
  • 77

2 Answers2

2

The special regular expression characters are:

. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

const a ="Test(123)";
console.log(a.replace(/\(/g, ''));

you need to use the escape char \ for this. there are set of char which need to be escaped in this replace(regex)

a.replace(/\(/g, '');

Find a full details here at MDN

Kaushik
  • 2,024
  • 1
  • 21
  • 29
0

you need to escape the ( with \ in a new variable because a is const and it will work

var b = a.replace(/\(/g, '');

for more practicing use this site regExr

Ali Abbas
  • 1,195
  • 9
  • 17