Home & blog  /  2010  /  Jun  /  view post  /

Match elements via REGEXP in jQuery

posted: 20 Jun '10 17:35 tags: REGEXP, jQuery, extension, Javascript

One of the odd omissions from jQuery's famous selectors is the ability to filter by regular expression. The usual work-around is to iteratively interrogate each chain element's text in a callback function inside .filter(), but this is a little clunky and feels unnecessasry.

Mitya to the rescue. I've just posted a plugin and core extension which allows precisely this, both via a pseudo selector, :regexp, and via a method, .regexp().

Head over here to download, get usage info or view a demo.

So for example, to use the pseudo selector:

$('selector:regexp(/pattern/)') //etc

or, to achieve the same effect with the method:

$(selector).regexp(/pattern/) //etc

Note that, to use the pseudo method, you'll need to double-escape any characters that need escaping. This is because you're specifying a REGEXP pattern inside a string.

post new comment

Comments (0)