0

I need to return true if string is containing either nothing or just space(s), else false.

var str = "";     // true
var str = "   ";  // true
var str = "  1 "; // false
var str = " s ";  // false

How can I do that?

I have tried using /^\s*?$/.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
stack
  • 10,280
  • 19
  • 65
  • 117

3 Answers3

1
/^ *$/
  • ^ - from the start
  • $ - til the end
  • * - the string consists of zero or more spaces

See it in action

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • @ndn Isn't `'\t'` empty or unicode white spaces? – Andreas Louv Jan 29 '16 at 23:00
  • @dev-null, in the comments, OP stated that he tried to use `\s` and didn't get the desired results. Having that in mind and the lack of negative feedback on his side, it is only reasonable to assume that when he says *spaces*, he **literally** means *spaces*. So to answer your question - no, a *tab* is not a *space*. – ndnenkov Jan 29 '16 at 23:51
  • @ndn You are right ... Just for my information, may you please tell me how can I add *tab* to that regex? – stack Jan 30 '16 at 00:16
  • 1
    @stack, you can use character set instead of a single space. A character set is defined by using `[]`. In this case `[ \t]` will match either space or tab. `\s` matches a few other kinds of whitespaces. – ndnenkov Jan 30 '16 at 07:52
1

I would replace the spaces with nothing and see what the length is.

var strTest = str.replace(" ","");
if(strTest.length == 0) {dowork();}

if the string is all spaces then the length will be 0.

Nefariis
  • 3,451
  • 10
  • 34
  • 52
1

You could also do:

if (!str || !str.replace(/ /g, "")) {
    // str is either empty, null, undefined or has nothing in it other than spaces
}

This also protects you if str is null or undefined too.

Here's a demo using the OP's test cases:

var testStrings = ["", "   ", "  1  ", " s "];

testStrings.forEach(function(str) {
  var result = false;
  if (!str || !str.replace(/ /g, "")) {
    // str is either empty, null, undefined or has nothing in it other than spaces
    result = true;
  }
  log('"' + str + '"' + " tests as " + result + "<br>");
});

function log(x) {
  var r = document.getElementById("results");
  var div = document.createElement("div");
  div.innerHTML = x;
  r.appendChild(div);
}
<pre id="results"></pre>
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • The regex you have is going to return true if there are any spaces, you need `^` and `$` to match the OPs requirement to match strings made up of only spaces. – Adam Konieska Jan 29 '16 at 22:39
  • @AdamKonieska - I don't think you realize what this is doing. I'm using `.replace()` so all that will be left is the non-space characters in the string and this simply tests is that string of non-space characters is empty. – jfriend00 Jan 29 '16 at 22:41
  • @jfriend00 you're right. I'd gotten it in my head that OP wanted `""` to be false when tested. – Adam Konieska Jan 29 '16 at 22:49
  • @AdamKonieska - You can just run the demo I added to my answer. – jfriend00 Jan 29 '16 at 22:50