javascript 如何使用 underscore.js 查找字符串是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25900248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:12:11  来源:igfitidea点击:

How to use underscore.js to find if a String is blank?

javascriptstringunderscore.js

提问by Venkat Sudheer Reddy Aedama

I am looking an equivalent of http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank(java.lang.CharSequence)I found several 3rd party extensions, but is there one out of the box with underscore.js: http://underscorejs.org

我正在寻找等效的http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank(java.lang.CharSequence)我发现了几个 3rd 方扩展,但是否有一个开箱即用的 underscore.js:http: //underscorejs.org

回答by Venkat Sudheer Reddy Aedama

Following @muistooshort's advice I created my mixin and wanted to share it:

按照@muistooshort 的建议,我创建了我的 mixin 并想分享它:

  _.mixin({
    isBlank: function(string) {
      return (_.isUndefined(string) || _.isNull(string) || string.trim().length === 0)
    }
  });

> _("\t").isBlank()
< true

> _("qwerty").isBlank()
< false

回答by Liam Middleton

_.isEmpty(_.trim(string));

This runs a null or empty string check.

这将运行 null 或空字符串检查。

Edited with update from ehftwelve

使用 ehftwelve 的更新进行编辑

回答by maerics

function isBlank(str) {
  return !!(str||'').match(/^\s*$/);
}

isBlank(null);    // => true
isBlank('');      // => true
isBlank(' \t ');  // => true
isBlank(' foo '); // => false

回答by taseenb

In Underscore.js you have _.isEmpty( http://underscorejs.org/#isEmpty) but it only checks if the array-like object or string has a length of 0.

在 Underscore.js 中有_.isEmptyhttp://underscorejs.org/#isEmpty),但它只检查类数组对象或字符串的长度是否为 0。

If you want to check if a variable is a whitespace string, empty string or null (equivalent of java's isBlank) you should make your own function.

如果要检查变量是空白字符串、空字符串还是 null(相当于 java 的 isBlank),您应该创建自己的函数。

function isBlank(str) {

  if (str === null || str.length === 0 || str === " ") return true;
  return false;

}

回答by dandavis

using bind to create a native method:

使用 bind 创建一个本地方法:

var isBlank=/./.test.bind(/(^$)|(^null$)|(^\s+$)/); // bind a regexp to test()

_.map(["", " ", " x ", 0, null], isBlank ); // test it against various data
// == [true, true, false, false, true] // reflects java version output

回答by Arman Ortega

Here's my snippet that I used when checking empty strings.

这是我在检查空字符串时使用的代码段。

    _.isBlank = function (str) {
       return (!str || /^\s*$/.test(str));
    }

    var strs = ['a', 1, -1, 0, NaN, '', ' ', '\t', '\n', null, undefined];
    strs.forEach(function(s){
     console.log('is blank? ', s, _.isBlank(s));
    });

Note: The zero(0) will be considered as empty.

注意:零(0)将被视为空。

回答by Matt

You could extend underscore.

你可以扩展下划线。

  _.isBlank = function (str) {
        return !!(str||'').match(/^\s*$/);
    }

If you really don't want to create a function as taseenb mentioned use isEmpty with trim. Although this is ugly!

如果您真的不想创建 taseenb 提到的函数,请使用 isEmpty with trim。虽然这很丑!

_.isEmpty('    '.trim());

Although both these don't work with tabbed spaces

虽然这两个都不适用于选项卡式空格