0

I'm looking for a way to remove these characters from a string in javascript (and spaces).

?[]/\=<>:;,'\"&$#*()|~`!{} 

I am unsure how to construct this:

"mystring is - ?[] hello ".replace(regex, ""); 

Some elements need escaping, some do not?

0

1 Answer 1

1

Inside a character class [], most don't need escaping:

var pattern = /[?\[\]/\\=<>:;,'"&$#*()|~`!{}]/g; "mystring is - ?[] hello ".replace(pattern, ""); 

The g flag is added for global replacement.

alert("mystring is -<> ;:,'\"&%^=!{} ?[] hello ".replace(pattern, "")); // Prints: mystring is - %^ hello 

Here it is in action

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

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.