Regular expression allowing trailing and leading spaces in an email address

Now I check the email addresses as follows:

[-+.'\w]+@[-.\w]+\.[-.\w]+

This means that if users accidentally have a trailing or leading space at this address (for example, when copying / pasting), the expression checks for false.

So, I want to allow the final and leading space in the above expression. How can i do this?

+3
source share
2 answers

Use \s*at the end and at the beginning of the regular expression. \s*means white spaces with zero or more cases.

+5
source

Consider the following regex ...

\s*[-+.'\w]+@[-.\w]+\.[-.\w]+\s*

Good luck

+1
source

All Articles