Finding spaces in text javascript

I have the following function and regular expression to find white spaces, but it returns true no matter what the text is.
var reWhitespace = /(\s)/;
var found = ABN.match(reWhitespace);
if(!found){}

Open in new window

LVL 1
Netlink2Asked:
Who is Participating?
 
quincydudeCommented:
this
ABN = " asdf";
var reWhitespace = /\s/g;
var found = ABN.match(reWhitespace);
if(!found){alert("true");}

Open in new window

0
 
Lolly-InkCommented:
Use the test method of RegExp to return true or false.
<html>
<script>
   var reWhitespace = /(\s)/;
   var ABN = ["Test 1", "Test2"];
   for (var index = 0; index < ABN.length; index++)
   {
      var found = reWhitespace.test(ABN[index]);
      alert("Whitespace " + (found ? "Found" : "Not Found") + ": " + ABN[index]);
   }
</script>
</html>

Open in new window

0
 
Netlink2Author Commented:
Sorry Lolly-Ink I didn't get a chance to try your solution, the pressure is on here :). But I'm sure I'll use your method later in my validation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.