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

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

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

阅读 21.2k
3 个回答

用正则表达式。

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")
}
keyWords.value.trim().length === 0

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题