Regular expression to allow trailing and leading spaces in email address

久未见 提交于 2019-12-04 04:42:52

问题


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?


回答1:


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




回答2:


Consider the following Regex...

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

Good Luck!



来源:https://stackoverflow.com/questions/22025168/regular-expression-to-allow-trailing-and-leading-spaces-in-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!