• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

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

0
Netlink2
Asked:
Netlink2
2 Solutions
 
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
 
quincydudeCommented:
this
ABN = " asdf";
var reWhitespace = /\s/g;
var found = ABN.match(reWhitespace);
if(!found){alert("true");}

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now