645

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.

0

10 Answers 10

771

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')
  .filter(function() {
    return this.id.match(/abc+d/);
  })
  .html("Matched!");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

2
  • how to use variables in the place of match(/abc+d/); ? Commented Jun 25, 2015 at 16:06
  • 2
    @Sasivarnakumar the answer for this question is here Commented Jul 26, 2018 at 22:24
361

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.

UPDATE: : syntax Deprecation JQuery 3.0

Since jQuery.expr[':'] used in Padolsey's implementation is already deprecated and will render a syntax error in the latest version of jQuery, here is his code adapted to jQuery 3+ syntax:

jQuery.expr.pseudos.regex = jQuery.expr.createPseudo(function (expression) {
    return function (elem) {
        var matchParams = expression.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(jQuery(elem)[attr.method](attr.property));
    }
});
6
  • 3
    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. Commented Oct 10, 2008 at 5:49
  • The regex selector by @padolsey works great. Here's an example where you can iterate over text, file and checkbox input fields or textareas with it: $j('input:regex(type, text|file|checkbox),textarea').each(function(index){ // ... }); Commented Jan 6, 2012 at 11:41
  • 17
    The answer below from nickf should be the accepted one. If you are reading this answer, be sure to read that one! Commented Sep 17, 2014 at 19:58
  • 6
    I'm getting Error: Syntax error, unrecognized expression: unsupported pseudo: regex Commented Jan 29, 2015 at 0:15
  • 2
    -1. The code to implement this is not included in the answer and is susceptible to link rot. Additionally, I found two bugs while testing the code - it will drop commas from regular expressions containing them (solved by replacing matchParams.join('') with matchParams.join(',')), and any pattern that matches 'undefined' or 'null' will match undefined and null, respectively. This second bug can be solved by checking that the tested value !== undefined and !== null first. Either way, passing a function into .filter() is easier and feels cleaner / more readable to me.
    – James T
    Commented Mar 2, 2018 at 22:49
356

These can be helpful.

If you're finding by Contains then it'll be like this

    $("input[id*='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you're finding by Starts With then it'll be like this

    $("input[id^='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you're finding by Ends With then it'll be like this

     $("input[id$='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which id is not a given string

    $("input[id!='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which name contains a given word, delimited by spaces

     $("input[name~='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen

     $("input[id|='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });
1
  • 5
    Great answer, but ids, being identifiers, can't contain a space, the ~= example should be changed to something else, like class, which is a white-space delimited list of identifiers. Things like class are what the ~= attribute selector was intended for. Commented Sep 25, 2016 at 14:29
66

If your use of regular expression is limited to test if an attribut start with a certain string, you can use the ^ JQuery selector.

For example if your want to only select div with id starting with "abc", you can use:

$("div[id^='abc']")

A lot of very useful selectors to avoid use of regex can be find here: http://api.jquery.com/category/selectors/attribute-selectors/

2
  • this won't work for case insensitive matches requirements. The .filter function better fits those needs.
    – brainondev
    Commented Aug 21, 2014 at 11:25
  • 3
    this was good for me, I just wanted to see if there was a '__destroy' on the end of an input id so I used *= like this: $("input[id*='__destroy'][value='true']") Commented Jan 29, 2015 at 0:25
25
var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);

Here you go!

9

Add a jQuery function,

(function($){
    $.fn.regex = function(pattern, fn, fn_a){
        var fn = fn || $.fn.text;
        return this.filter(function() {
            return pattern.test(fn.apply($(this), fn_a));
        });
    };
})(jQuery);

Then,

$('span').regex(/Sent/)

will select all span elements with text matches /Sent/.

$('span').regex(/tooltip.year/, $.fn.attr, ['class'])

will select all span elements with their classes match /tooltip.year/.

7

ids and classes are still attributes, so you can apply a regexp attribute filter to them if you select accordingly. Read more here: http://rosshawkins.net/archive/2011/10/14/jquery-wildcard-selectors-some-simple-examples.aspx

0
2
$("input[name='option[colour]'] :checked ")
2

I'm just giving my real time example:

In native javascript I used following snippet to find the elements with ids starts with "select2-qownerName_select-result".

document.querySelectorAll("[id^='select2-qownerName_select-result']");

When we shifted from javascript to jQuery we've replaced above snippet with the following which involves less code changes without disturbing the logic.

$("[id^='select2-qownerName_select-result']")

0
1

If you just want to select elements that contain given string then you can use following selector:

$(':contains("search string")')

Not the answer you're looking for? Browse other questions tagged or ask your own question.