views:

30814

answers:

5

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with the 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.

+35  A: 

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.

Xenph Yan
Ok. I have been there but I didn't really know the name of what I was looking for. Ive had another look and using attribute filters is what I was after.
Joel Cunningham
+37  A: 

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>

$('div')
    .filter(function() {
        return this.id.match(/abc+d/);
    })
    .html("Matched!")
;
nickf
+4  A: 

After a while you get to understand that jQuery allows the use of regular expressions in a lot of places, I.E. when selecting like this:

$('#myElement').html();

So with this in mind, you can use \S* to make wildcard selections, I.E.:

$('#myEle\\S*').each();

From: http://colourgray.wordpress.com/2008/08/05/jquery-wildcard-selectors/

Robert MacLean
wildcards are very cool but they aren't quite "regular expressions"
Jeff Atwood
That site says the wildcards don't work anymore
fudgey
A: 
$("input[name='option[colour]'] :checked ")
irfan akhtar
A: 

ids and classes are still attributes, so you can apply a regexp attribute filter to them if you select accordingly. Read more here: http://www.insideria.com/2009/04/jquery-wild-card-example.html

Janis