Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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-]+$

Valid case

String123/test //string without space

Invalid case

String123/ test // contains space in between string 

String 123/test // contains space in between string 

 String123/test  // contains leading\trailing space

i.e; I have to white list strings which does not contain any spaces.

like image 923
Justin Avatar asked Feb 10 '17 09:02

Justin


1 Answers

You may use \S

\S matches any non white space character

Regex

/^\S+$/

Example

function CheckValid(str){
   re = /^\S+$/
   return re.test(str)
 }


console.log(CheckValid("sasa sasa"))
console.log(CheckValid("sasa/sasa"))                      
console.log(CheckValid("sas&2a/sasa"))                      
                       
like image 55
Sarath Sadasivan Pillai Avatar answered Oct 10 '22 22:10

Sarath Sadasivan Pillai