I am looking for a function that will return true / false if the string in question is complied of any number of spaces. The common method of doing this is if (string == " ") but this only works for one space. I need a function that would resolve any number of spaces to a return statement.
example
if (string == " ")
if (string == " ")
if (string == " ")
if (string == " ")
if (string == " ")
What is the best way to do this?
You can use a regular expression:
/^ +$/.test(string).A regular expression is also good if you want to match any whitespace rather than just spaces (which is sometimes useful):
/^\s+$/.test(string). The\smatches all whitespace characters like" "and"\t". So:For reference,
"\t"will look something like(I think SO turned the tab into spaces, but that’s more or less what it would look like.)