Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created December 17, 2009 22:12
Show Gist options
  • Save robertsosinski/259072 to your computer and use it in GitHub Desktop.
Save robertsosinski/259072 to your computer and use it in GitHub Desktop.
Custom jQuery RegEx Filter
<!DOCTYPE html>
<html>
<head>
<title>RegEx jQuery Filter</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
(function($){
$.expr[':'].regex = function(elem, index, match) {
var matchParams = match[3].split(','),
validLabels = /^(data|css):/,
attr = {
method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr',
property: matchParams.shift().replace(validLabels,'')
},
regexFlags = 'ig',
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);
return regex.test($(elem)[attr.method](attr.property));
}
$(function() {
$("li:regex(id, [0-9])").each(function() {
console.log($(this).text());
});
$("li:regex(id, ^[aeiouz])").each(function() {
console.log($(this).text());
});
});
})(jQuery);
</script>
</head>
<body>
<ul>
<li id="123">123</li>
<li id="oneTwoThree">oneTwoThree</li>
<li id="0">0</li>
<li id="zero">zero</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment