正则表达式:允许字母,数字和空格(至少包含一个字母或数字) [英] Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)
问题描述
我目前正在使用此正则表达式 ^ [A-Z0-9 _] * $
接受字母,数字,空格和下划线。我需要修改它以在字符串中的某处需要至少一个数字或字母。任何帮助将不胜感激!
I'm currently using this regex ^[A-Z0-9 _]*$
to accept letters, numbers, spaces and underscores. I need to modify it to require at least one number or letter somewhere in the string. Any help would be appreciated!
这将用于验证我的网站的用户名。我实际上想支持尽可能多的字符,但只是想确保我阻止代码注入,并且字符对所有用户都显示正常。所以我肯定对正则表达式验证建议开放,这些建议可以支持更广泛的字符集。
This would be for validating usernames for my website. I'd actually like to support as many characters as I can, but just want to ensure that I prevent code injection and that characters will display fine for all users. So I'm definately open to regex validation suggestions that would support a wider set of characters.
推荐答案
你只需要指定你的当前RE,后跟一个字母/数字,然后是您当前的RE:
You simply need to specify your current RE, followed by a letter/number followed by your current RE again:
^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$
既然你已经现在声明他们是Javascript RE,有一个有用的网站这里你可以测试RE反对输入数据。
Since you've now stated they're Javascript REs, there's a useful site here where you can test the RE against input data.
如果你想要小写字母:
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
这篇关于正则表达式:允许字母,数字和空格(至少包含一个字母或数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!