Ven*_*ama 11

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

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

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

> _("qwerty").isBlank()
< false
Run Code Online (Sandbox Code Playgroud)


Lia*_*ton 8

_.isEmpty(_.trim(string));
Run Code Online (Sandbox Code Playgroud)

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

编辑来自ehftwelve的更新

  • 好吧,这样做:`_.isEmpty(_.trim(string))` (11认同)

mae*_*ics 5

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

isBlank(null);    // => true
isBlank('');      // => true
isBlank(' \t ');  // => true
isBlank(' foo '); // => false
Run Code Online (Sandbox Code Playgroud)

  • 此方法还为“isBlank(0);”和“isBlank(false);”返回“true”。这可能不是您想要的。 (2认同)