正则表达式 - 匹配一个没有空格的字符串 [英] Regex - match a string without having spaces
本文介绍了正则表达式 - 匹配一个没有空格的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
构建一个将拒绝包含空格的输入字符串的正则表达式。
Building an regular expression that will reject an input string which contain spaces.
我有一个以下表达式,但它不能正常工作;
I have a following expression, but its not working as well;
^[a-zA-Z0-9!@#*()+{}[\\];:,|\/\\\\_\S-]+$
有效案例
String123/test //string without space
无效案例
String123/ test // contains space in between string
String 123/test // contains space in between string
String123/test // contains leading\trailing space
ie;我必须使用不包含任何空格的白名单字符串。
i.e; I have to white list strings which does not contain any spaces.
推荐答案
您可以使用 \ S
\S
匹配任何非空格字符
正则表达式
/^\S+$/g
示例
function CheckValid(str){
re = /^\S+$/g
return re.test(str)
}
console.log(CheckValid("sasa sasa"))
console.log(CheckValid("sasa/sasa"))
console.log(CheckValid("sas&2a/sasa"))
这篇关于正则表达式 - 匹配一个没有空格的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文