-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
  • 183,571
  • 29
  • 224
  • 209
Marco Jr
  • 6,496
  • 11
  • 47
  • 86

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,072
  • 1
  • 23
  • 31
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,415
  • 12
  • 19