JS判断字符串是否为空或是否全为空格

Posted ~泊水~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS判断字符串是否为空或是否全为空格相关的知识,希望对你有一定的参考价值。

 

var test = "      ";
//为空或全部为空格
if (test.match(/^[ ]*$/)) {
        console.log("all space or empty");
}

 

var test = "   
   ";
//var test = "      ";
if(test.match(/^s+$/)){
    console.log("all space or \n")
}
if(test.match(/^[ ]+$/)){
    console.log("all space")
}
if(test.match(/^[ ]*$/)){
    console.log("all space or empty")
}
//空|空格|换行
if(test.match(/^s*$/)){
    console.log("all space or \n or empty")
}

 

以上是关于JS判断字符串是否为空或是否全为空格的主要内容,如果未能解决你的问题,请参考以下文章