Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow trailing and leading spaces

I now validate email addresses like so:

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

Which means that if users accidentally have a trailing or leading space in that address (e.g. when copy/pasting), the expression validates to false.

So I want to allow trailing and leading spaces on the above expression. How can I do so?

like image 969
Adam Avatar asked Feb 25 '14 20:02

Adam


2 Answers

Use \s* at the end and start of your regex. \s* means white spaces having zero or more occurrence.

like image 170
Sabuj Hassan Avatar answered Nov 10 '22 11:11

Sabuj Hassan


Consider the following Regex:

\s*[-+.'\w]+@[-.\w]+\.[-.\w]+\s*
like image 31
gpmurthy Avatar answered Nov 10 '22 09:11

gpmurthy