在 JavaScript 中是否有类似于
string.Empty
的方法,或者只需要检查 ""
是否为空?string.Empty
的方法,或者只需要检查 ""
是否为空?以下正则表达式是另一种解决方法,可用于空、空字符串或未定义的字符串。
(/(null|undefined|^$)/).test(null)
我添加了这个解决方案,因为它可以进一步扩展以检查空值或一些像下面这样的值。以下正则表达式正在检查字符串是否为空、null、undefined或仅包含整数。
(/(null|undefined|^$|^\d+$)/).test()
(/(null|undefined|^$)/).test("null")
- sean_.isEmpty()
函数,用于检查空字符串和其他空对象。其他非常有用的 Underscore.js 函数包括:isEmpty
_.isEmpty(object)
如果枚举对象不包含值(没有可枚举的自有属性),则返回 true。对于字符串和类数组对象,_.isEmpty 检查长度属性是否为 0。
_.isEmpty([1, 2, 3]);
=> false
_.isEmpty({});
=> true
_.isNull(object)
_.isUndefined(value)
_.has(object, key)
这是一个假值。
第一个解决方案:
const str = "";
return str || "Hello"
const str = "";
return (!!str) || "Hello"; // !!str is Boolean
const str = "";
return (+str) || "Hello"; // !!str is Boolean
NaN
,这也是“假值”。 - devildeltaisBlank 函数的最终和最短变体:
/**
* Will return:
* False for: for all strings with chars
* True for: false, null, undefined, 0, 0.0, "", " ".
*
* @param str
* @returns {boolean}
*/
function isBlank(str){
return (!!!str || /^\s*$/.test(str));
}
// tests
console.log("isBlank TRUE variants:");
console.log(isBlank(false));
console.log(isBlank(undefined));
console.log(isBlank(null));
console.log(isBlank(0));
console.log(isBlank(0.0));
console.log(isBlank(""));
console.log(isBlank(" "));
console.log("isBlank FALSE variants:");
console.log(isBlank("0"));
console.log(isBlank("0.0"));
console.log(isBlank(" 0"));
console.log(isBlank("0 "));
console.log(isBlank("Test string"));
console.log(isBlank("true"));
console.log(isBlank("false"));
console.log(isBlank("null"));
console.log(isBlank("undefined"));
我更喜欢使用“非空测试”而不是“空白测试”
function isNotBlank(str) {
return (str && /^\s*$/.test(str));
}
function tell()
{
var pass = document.getElementById('pasword').value;
var plen = pass.length;
// Now you can check if your string is empty as like
if(plen==0)
{
alert('empty');
}
else
{
alert('you entered something');
}
}
<input type='text' id='pasword' />
还有一个好主意是检查您是否尝试传递未定义的术语。
function TestMe() {
if((typeof str != 'undefined') && str) {
alert(str);
}
};
TestMe();
var str = 'hello';
TestMe();
我经常遇到这样的情况:当一个对象实例的字符串属性不为空时,我想要做一些事情。问题是,该属性并不总是存在。
var myString = 'hello';
if(myString.charAt(0)){
alert('no empty');
}
alert('empty');
使用核心(即原始)JavaScript,我们可以利用Object.is()进行严格的相等比较。以下是代码片段。
function validateString(arg) {
if (Object.is(arg, "") || Object.is(arg, null) || Object.is(arg, undefined)) {
return false;
}
return true;
}
这里是 JavaScript 规范: https://262.ecma-international.org/12.0/#sec-object.is
这里是 Mozilla 文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
希望这能有所帮助。
这里是我用于处理此问题的一些自定义函数。以及代码运行示例。
const v1 = 0
const v2 = '4'
const v2e = undefined
const v2e2 = null
const v3 = [1, 2, 3, 4]
const v3e = []
const v4 = true
const v4e = false
const v5 = {
test: 'value'
}
const v5e = {}
const v6 = 'NotEmpty'
const v6e = ''
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}
function isEmpty(v, zeroIsEmpty = false) {
/**
* When doing a typeof check, null will always return "object" so we filter that out first
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null
*/
if (v === null) {
return true
}
if (v === true) {
return false
}
if (typeof v === 'object') {
return !Object.keys(v).length
}
if (isNumeric(v)) {
return zeroIsEmpty ? parseFloat(v) === 0 : false
}
return !v || !v.length || v.length < 1
}
console.log(isEmpty(v1), isEmpty(v1, true))
console.log(isEmpty(v2), isEmpty(v2e), isEmpty(v2e))
console.log(isEmpty(v3), isEmpty(v3e))
console.log(isEmpty(v4), isEmpty(v4e))
console.log(isEmpty(v5), isEmpty(v5e))
console.log(isEmpty(v6), isEmpty(v6e))
另外作为参考,这里是Lodash isEmpty的源代码:
null
或undefined
是空的呢?一个空字符串就是一个空字符串,它不是null
或undefined
。 - Flimm