Regex - match a string without having spaces

前端 未结 2 1186
孤独总比滥情好
孤独总比滥情好 2020-12-11 20:08

Building an regular expression that will reject an input string which contain spaces.

I have a following expression, but its not working as well;

^[         


        
2条回答
  •  盖世英雄少女心
    2020-12-11 20:49

    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"))                      
                           

提交回复
热议问题