Home & blog  /  Scripts  /  view post  /

Match elements via REGEXP in jQuery

Overview

This script allows REGEXP matching as part of jQuery's selector chains. It includes a plugin which provides a .regexp() method as part of a chain, and also an extension, which provides a :regexp pseudo selector.

Usage

Like other jQuery filters such as contains(), this REGEXP filter can be applied either as a pseudo selector or as a separate method of the chain. For the former, it is used as follows:

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

Important: because arguments passed to pseudo-selectors (like our 'pattern' above) are sent as strings, any escaped characters must be double escaped. See example for more info.

Alternatively, the .regexp() method is used as follows:

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

Note that, unlike with the pseudo selector technique, we are passing a genuine REGEXP object as the argument, so you do not have to double escape.

Example

  • PS3 games
  • PSP games
  • PS2 games
  • Nintendo Wii games
  • Nintendo DS games
  • XBox games

Click here to highlight Playstation rows

Clicking the above link highlights only the <li>s that match the selector. Here's the code behind the <a>'s onclick argument:

$('#productCats li:regexp(/^PS[\\d|P]{1}/)').css('color', '#f00')

This example uses the pseudo selector (note double escape), but we could achieve the same effect by using the equivalent method:

$('#productCats li').regexp(/^PS[\d|P]{1}/).css('color', '#f00')

Notes

Pseudo vs. method

Many jQuery filters can be implemented in either pseudo selector or method form. For example, there is both a :not pseudo selector and a not() method. Usually, the method alternative allows for more flexability as it accepts more arguments. Here, though, there is no difference in the workings of :regexp and .regexp().

regexp vs. contains

To match a simple string against the text of an element, rather than match a regular expression, you can use jQuery's in-built :contains pseudo selector or .contains() method.

Loading jQuery

By default, this script assumes you're loading jQuery via Google (why might you do this?). If, instead, you're loading jQuery from a local copy in your site folder, update the document ready handler (DRH) in the script file:

1//loading jQuery via Google

2google.setonloadcallback(function() { ...

3 

4//loading jQuery from local copy (use either)

5$(document).ready(function() {...

6$(function() {...

post new comment

Comments (2)

Mario, at 9/03/'11 22:29, said:
I've downloaded latest unpacked version. In my xhtml i've put this: 
<p><input type="button" value="Enable / Disable Language"></p>
When i'm trying to use :regexp(pattern) it calls for some unexisted function a():
Also I can't bring to work .regexp(pattern) - casue function doesnt exists ...
Mitya, at 10/03/'11 20:39, said:
It sounds as though you are not including my script. Run this:

alert($.fn.regexp);

If you get null or undefined, the script isn't being loaded. It works on this page.