Mac*_*ony 89
这个问题用jQuery标记.在jQuery中,您可以运行以下命令:
if ( $.trim( $('#myInput').val() ) == '' )
alert('input is blank');
Run Code Online (Sandbox Code Playgroud)
Cre*_*esh 36
/^\s+$/.test(userText)
将+to 更改*为包含空字符串''作为正匹配.
编辑
通常情况下,您需要从用户输入的文本中修剪空白,并测试它是否为非空:
userText = userText.replace(/^\s+/, '').replace(/\s+$/, '');
if (userText === '') {
// text was all whitespace
} else {
// text has real content, now free of leading/trailing whitespace
}
Run Code Online (Sandbox Code Playgroud)
joe*_*t45 23
这也有效:
var text = " ";
text.trim().length == 0; //true
Run Code Online (Sandbox Code Playgroud)
像这样...
function isEmpty(str) {
return str.replace(/^\s+|\s+$/g, '').length == 0;
}
Run Code Online (Sandbox Code Playgroud)
如果您想查看文件是否包含所有空格还是空白,我建议您测试反转并反转结果.这样你就不必担心空字符串周围的特殊情况.
所有空格都与非空格相同,因此:
function isWhitespaceOrEmpty(text) {
return !/[^\s]/.test(text);
}
Run Code Online (Sandbox Code Playgroud)
如果您不想要空字符串,可以稍微修改它:
function isWhitespaceNotEmpty(text) {
return text.length > 0 && !/[^\s]/.test(text);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84126 次 |
| 最近记录: |