Grep and regular expression in jQuery

Description

The following code shows how to grep and regular expression.

Example


<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--   w  w w .j a  v  a 2 s  .com-->
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>

Click to view the demo

The code above generates the following result.

Grep and regular expression in jQuery
Home »
  Javascript Tutorial »
    jQuery »
      jQuery Data
Javascript Tutorial jQuery Data
Grep an array by checking element value and...
Grep an array by element value in jQuery
Grep an array with index by using a functio...
Grep and regular expression in jQuery
Use the built-in reverse-method to reverse ...