For example, I need to search 'abc-def+1@email.com'. Now the email input is case sensitive from the user and it should match to 'Abc-Def+1@gmail.com' I tried using regex, but it is not working in the case of + and - signs in between. Please help.
1 Answers
+
and .
are special characters in regular expressions and need to be escaped to behave how you're expecting.
If your regular expression is /abc-def+1@gmail.com/i
then it will match up to Abc-Def
then fail on the +
. In regular expressions, +
means to match 1 or more
of the previous character. So in this case it is looking for at least one f
then a 1
. To prevent this you would put a \
in front of the +
like so: /abc-def\+1@gmail.com/i
.
The .
character will match any character, and might give you different results than you want. If we take the previous regular expression, /abc-def\+1@gmail.com/i
you can see .
hasn't been escaped. This means that it could match a string like abc-def+1@gmailgcom
. Just like before, this would need to be escaped, resulting in a regular expression like this: /abc-def\+1@gmail\.com/i
.
It seems like you're taking in user input for this query, so you'll need to escape any special characters present before querying the database.

- 3,806
- 2
- 10
- 24