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

本文是小编为大家收集整理的关于JS如何检查空字符串和空间的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

帮助我

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

,但我需要检查空间""(2,3,...空间包括在内)是相同的空字符串

ps.对不起,我的英语

推荐答案

正时正面可以轻松解决此问题.

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

如果您还需要包括null,请添加!value ||.

如果您需要包含newlines,Tabs等,请使用/^\s*$/进行正则.

其他推荐答案

我不确定我是否正确理解.你的意思是:

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

本文地址:https://www.itbaoku.cn/post/1938237.html

问题描述

Help me pls

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

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

ps. sorry in my English

推荐答案

A regex can easily solve this problem.

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

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

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

其他推荐答案

I'm not sure I'm understanding correctly. Do you mean like:

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