How to remove brackets in a regex?

0

I'm using a regex to remove some characters, but as one of them is the brackets [] and I'm having problems with the regex replace(/[.!'@,><|://\;&*()_+=]/g, ""); . How would it be to remove this character in this function?

    
asked by anonymous 30.05.2017 / 22:23

3 answers

3

I just added it at the beginning [e].

It looks like this:

replace(/[\[\].!'@,><|://\;&*()_+=]/g, "")
    
30.05.2017 / 22:31
0

Use scape ( \ ), would look like this:

/\[.!'@,><|://\;&*()_+=\]/g

Example: link

    
30.05.2017 / 22:28
0
  

About regular expressions in general, for uses other than JavaScript

Always walk with the Aurelio Verde's regular expression guide (direct link to the list part). You can by closing the bracket as a special character shortly after opening a list or a denied list; for example []abc] , enter the following characters:

  • ]
  • a
  • b
  • c

The open brackets can be placed anywhere, it will not undergo special treatment.

I mention the special positioning because there are regular expression processors that do not accept escape within lists or denied lists, so the regular expression [abc\]] is not treated in a way equivalent to the one I wrote previously []abc] .

As much as the focus of the question is JavaScript, it is always good to have a general idea not to be surprised when using regular expressions in other places.

    
31.05.2017 / 01:27