JS如何检查空字符串和空格 [英] JS how to check empty String and Space

查看:36
本文介绍了JS如何检查空字符串和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我

if(value == ""){
// do anything
}

但是我需要检查空格" " (2,3,... space is include) 是否与空字符串相同

but I need to check space " " (2,3,... space is include) is the same way of empty String

ps.对不起我的英语

推荐答案

正则表达式可以轻松解决这个问题.

A regex can easily solve this problem.

if (/^ *$/.test(value)) {
    //string contains 0+ spaces only
}

如果您还需要包含 null,则添加 !value ||.

If you need to include null also, then add !value ||.

如果您需要包含换行符、制表符等,请使用 /^\s*$/ 作为正则表达式.

If you need to include newlines, tabs, and the like, then use /^\s*$/ for the regex.

这篇关于JS如何检查空字符串和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆