首页 > js怎么判断字符串是不是全是空格

js怎么判断字符串是不是全是空格

<input type="text" name="keyWords" autocomplete="off" id="keyWords" >

input框中输入,怎么判断是不是全部输入的都是空格


keyWords.value.trim().length === 0

用正则表达式。

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

input.value.length>0 && input.value.trim().length == 0

【热门文章】
【热门文章】