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

2025-01-26 04:07:30
推荐回答(2个)
回答1:

e.replace('\s+', ''); //关键是这一句,把value中的空格全replace成空字符串 if(value.length > 0) { } els

回答2:

用正则表达式实现:
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")
}