0

For example, I need to search '[email protected]'. Now the email input is case sensitive from the user and it should match to '[email protected]' I tried using regex, but it is not working in the case of + and - signs in between. Please help.

1 Answers1

0

+ and . are special characters in regular expressions and need to be escaped to behave how you're expecting.

If your regular expression is /[email protected]/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\[email protected]/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\[email protected]/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.

Montgomery Watts
  • 3,806
  • 2
  • 10
  • 24