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

 

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

 

var test = "   \n   ";
//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")
}

 

posted on 2018-09-30 16:47  LJD泊水  阅读(18913)  评论(1编辑  收藏  举报