参考一:
function isEmpty(obj){ if(typeof obj == "undefined" || obj == null || obj == ""){ return true; }else{ return false; } }
参考二:
if (variable1 !== null || variable1 !== undefined || variable1 !== '') { var variable2 = variable1; }
var variable2 = variable1 || '';
var exp = null; if (exp == null) { alert("is null"); }
var exp = null; if (!exp) { alert("is null"); }
var exp = null; if (typeof exp == "null") { alert("is null"); }
var exp = null; if (isNull(exp)) { alert("is null"); }
var strings = ''; if (string.length == 0) { alert('不能为空'); }
var strings = ' '; if (strings.replace(/(^s*)|(s*$)/g, "").length ==0) { alert('不能为空'); }
另: str.replace(/(^\s*)|(\s*$)/g, "")) != ""
参考三:
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 很多情况下,都是用length来直接判断字符串是否为空,如下:
var strings = ”;
if (string.length == 0)
{
alert(‘不能为空’);
}
但如果用户输入的是空格,制表符,换页符呢?这样的话,也是不为空的,但是这样的数据就不是我们想要的吧。
其实可以用正则表达式来把这些“空”的符号去掉来判断的
var strings = ‘ ‘;
if (strings.replace(/(^\s*)|(\s*$)/g, “”).length ==0)
{
alert(‘不能为空’);
}
\s 小写的s是,匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
参考四:
判断字符串是否为空
var strings = ''; if (string.length == 0) { alert('不能为空'); }
判断字符串是否为“空”字符即用户输入了空格
var strings = ' '; if (strings.replace(/(^s*)|(s*$)/g, "").length ==0) { alert('不能为空'); }
判断输入字符串是否为空或者全部都是空格
function isNull( str ){ if ( str == "" ) return true; var regu = "^[ ]+$"; var re = new RegExp(regu); return re.test(str); }
如果有null时上面代码就无法正常判断了,下面代码是判断为null的情况
var exp = null; if (exp == null) { alert("is null"); }
exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。
注意:要同时判断 null 和 undefined 时可使用本法。 代码如下
var exp = null; if (!exp) { alert("is null"); }
如果 exp 为 undefined,或数字零,或 false,也会得到与 null 相同的结果,虽然 null 和二者不一样。注意:要同时判断 null、undefined、数字零、false 时可使用本法。代码如下
var exp = null; if (typeof exp == "null") { alert("is null"); }
为了向下兼容,exp 为 null 时,typeof null 总返回 object,所以不能这样判断
参考五:
方法一: 使用trim()
/* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) { let str = test.trim(); if (str.length == 0) { console.log('字符串全是空格'); } else { console.log('输入的字符串为:' + test); } }
如果 trim() 不存在,可以在所有代码前执行下面代码
/* 给String原型链对象添加方法trim */ if (!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }; }
例如:
/* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) { /* 给String原型链对象添加方法trim */ if (!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }; } let str = test.trim(); if (str.length == 0) { console.log('字符串全是空格'); } else { console.log('输入的字符串为:' + test); } }
方法二: 使用正则表达式
/* 使用正则表达式来判断字符串是否全为空 */ function kongge2(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") } else { console.log('输入的字符串为:' + test); } }
案例:
js判断字符串是否全为空(使用trim函数/正则表达式) 姓名:
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203364.html原文链接:https://javaforall.net
