0

This is my RegEx:

console.log(test.replace(/0098/g, ")").replace(/[()]/g, ''));

How can I replace only () if exists?

My code replace now all bridgets. I want to replace only where is exists ().

Like my example:

var test = "(test1234) - test948 () usi27361() 928372da";

New string: (test1234) - test948 usi27361 928372da

3
  • I think you are looking to escape those characters. Maybe > \(\) Commented Feb 25, 2020 at 15:17
  • [()] matches both characters even when they are 'alone'. You want to match the string '()' - That's what Yosef's answer below does. Commented Feb 25, 2020 at 16:15
  • To rephase @PoulBak comment above (not incorrect, just rephrasing it) - [xyz] - means match any one of the characters in the [] - it's the equivalent of x or y or z. Commented Feb 25, 2020 at 16:39

1 Answer 1

2

var str = "(test1234) - test948 () usi27361() 928372da";
console.log(str.replace(/\(\)/g, ""));

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.