正则表达式,不允许输入字段中的空格 [英] Regular expression for not allowing spaces in the input field

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

问题描述

我的表单中有一个用户名字段。我想不允许在字符串中的任何位置使用空格。我使用了这个正则表达式:

I have a username field in my form. I want to not allow spaces anywhere in the string. I have used this regex:

var regexp = /^\S/;

如果角色之间有空格,这对我有用。即如果用户名是 ABC DEF 。如果一个空间在开头,它就不起作用,例如<空><空> ABC 。正则表达式应该是什么?

This works for me if there are spaces between the characters. That is if username is ABC DEF. It doesn't work if a space is in the beginning, e.g. <space><space>ABC. What should the regex be?

推荐答案

虽然你已经指定了开始锚点和第一个字母,你没有为字符串的其余部分做任何事情。您似乎想要重复 字符类,直到字符串的结尾

While you have specified the start anchor and the first letter, you have not done anything for the rest of the string. You seem to want repetition of that character class until the end of the string:

var regexp = /^\S*$/; // a string consisting only of non-whitespaces

这篇关于正则表达式,不允许输入字段中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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