-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

4
  • 4
    escape it? /\(/g Commented Dec 13, 2019 at 11:42
  • 2
    a.replace(/\(/g, '') escape Commented Dec 13, 2019 at 11:42
  • Does this answer your question? How to escape regular expression special characters using javascript? Commented Dec 13, 2019 at 11:44
  • 1
    it should also be let a = "Test(123)" Commented Dec 13, 2019 at 11:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.