Grep and regular expression : Grep « jQuery « JavaScript Tutorial






<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

      var testData = [];
      var pattern = /^\d{5}(-\d{4})?$/;

      var originalArray = ['11111','abcde','1asdfasdf','asdfasdf-0339'];

      var badZips = $.grep(
                      originalArray,
                      function(value) {
                         return value.match(/^\d{5}(-\d{4})?$/) != null;
                       },
                       true);
      alert(badZips);
        });
    </script>

  </head>
  <body>
    <body>
        <p id="followMe">Follow me!</p>
    </body>
</html>








30.46.Grep
30.46.1.Grep callback
30.46.2.Grep value
30.46.3.Grep with index
30.46.4.Grep and regular expression