Friday, June 1, 2012

jQuery selector regular expressions

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector.

I have looked for this myself but have been unable to find information on the syntax and how to use it. Does anyone know where the documentation for the syntax is?

EDIT: The attribute filters allow you to select based on patterns of an attribute value.

Source: Tips4all


  1. James Padolsey created a wonderful filter that allows regex to be used for selection.

    Say you have the following div:

    <div class="asdf">

    Padolsey's :regex filter can select it like so:

    $("div:regex(class, .*sd.*)")

    Also, check the official documentation on selectors.

  2. You can use the filter function to apply more complicated regex matching. Here's an example which would just match the first three divs.

    <div id="abcd"></div>
    <div id="abccd"></div>
    <div id="abcccd"></div>
    <div id="abd"></div>

    .filter(function() {

  3. var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);

    Here you go!

  4. $("input[name='option[colour]'] :checked ")

  5. ids and classes are still attributes, so you can apply a regexp attribute filter to them if you select accordingly. Read more here: