正则表达式以允许尾随和前导空格 [英] Regular expression to allow trailing and leading spaces

查看:40
本文介绍了正则表达式以允许尾随和前导空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在像这样验证电子邮件地址:

I now validate email addresses like so:

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

这意味着,如果用户不小心在该地址中有尾随或前导空格(例如,在复制/粘贴时),则表达式会验证为false.

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?

推荐答案

在正则表达式的开头和末尾使用 \ s * . \ s * 表示出现零次或多次的空白.

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

这篇关于正则表达式以允许尾随和前导空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆