0

In javascript, what is the regex for alone spaces, not spaces between characters?

\s+ works for all whitespace.

What I am trying to do is if the string is just whitespace and not white-space with other characters, then do something.

Here is what I have so far: if(/regexp/i.exec(str) !== null) // do something

MrGuru
  • 325
  • 5
  • 15

2 Answers2

2

Try something like:

if (/^\s+$/i.test(str)) {
    // string contains only whitespaces
}
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
0
if(str.replace(/\s/g, "").length===0)
{


}

Replaces all the white spaces left and if no other character is left then the length should be 0

Srinath Mandava
  • 3,384
  • 2
  • 24
  • 37