What's the regular expression that matches a square bracket?

Viewed 260458

I want a regex that matches a square bracket [. I haven't found one yet. I think I tried all possibilities, but haven't found the right one. What is a valid regex for this?

8 Answers

How about using backslash \ in front of the square bracket. Normally square brackets match a character class.

Are you escaping it with \?

/\[/

Here's a helpful resource to get started with Regular Expressions:

Regular-Expressions.info

In general, when you need a character that is "special" in regexes, just prefix it with a \. So a literal [ would be \[.

does it work with an antislash before the [ ?

\[ or \\[ ?

Related