18

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

3
  • @Andrew - It's not a duplicate at all. That other question has nothing to do with detecting strings containing only spaces. Commented Apr 8, 2016 at 3:40
  • @RJM There are numerous answers on that question that handle: null/undefined and only-whitespace input. See, for example, this answer Commented Apr 8, 2016 at 3:45
  • sorry man I need to check String with whitespace only is same as empty String Commented Apr 8, 2016 at 3:45

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Why regex and not if (!value || !value.trim()) { //null or empty }. Matches empty strings, tabs, newline characters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.