Create a gist now

Instantly share code, notes, and snippets.

@Mottie /gist:461488
Last active Apr 14, 2016

What would you like to do?
Improved jQuery :contains() selector
/* jQuery selector to match exact text inside an element
* http://wowmotty.blogspot.com/2010/05/jquery-selectors-adding-contains-exact.html
* :containsExact() - case insensitive
* :containsExactCase() - case sensitive
* :containsRegex() - set by user ( use: $(el).find(':containsRegex("/(red|blue|yellow)/gi")') )
*/
$.extend( $.expr[":"], {
containsExact: $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elem) {
return $.trim(elem.innerHTML.toLowerCase()) === text.toLowerCase();
};
}) :
// support: jQuery <1.8
function(elem, i, match) {
return $.trim(elem.innerHTML.toLowerCase()) === match[3].toLowerCase();
},
containsExactCase: $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elem) {
return $.trim(elem.innerHTML) === text;
};
}) :
// support: jQuery <1.8
function(elem, i, match) {
return $.trim(elem.innerHTML) === match[3];
},
containsRegex: $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
var reg = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/.exec(text);
return function(elem) {
return reg ? RegExp(reg[1], reg[2]).test($.trim(elem.innerHTML)) : false;
};
}) :
// support: jQuery <1.8
function(elem, i, match) {
var reg = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/.exec(match[3]);
return reg ? RegExp(reg[1], reg[2]).test($.trim(elem.innerHTML)) : false;
}
});
@Mottie

This comment has been minimized.

Show comment Hide comment
@Mottie

Mottie Jul 24, 2010

Updated to use innerHTML to prevent matches on nested tags

Owner

Mottie commented Jul 24, 2010

Updated to use innerHTML to prevent matches on nested tags

@Mottie

This comment has been minimized.

Show comment Hide comment
@Mottie

Mottie Oct 4, 2010

Added a ":containsRegex" selector which also allows you to add flags. See this demo page.

Owner

Mottie commented Oct 4, 2010

Added a ":containsRegex" selector which also allows you to add flags. See this demo page.

@Mottie

This comment has been minimized.

Show comment Hide comment
@Mottie

Mottie Jan 19, 2012

Updated again with a note about double escaping any regex and added a check on the "reg" variable in case it is null.

Owner

Mottie commented Jan 19, 2012

Updated again with a note about double escaping any regex and added a check on the "reg" variable in case it is null.

@Mottie

This comment has been minimized.

Show comment Hide comment
@Mottie

Mottie Aug 30, 2012

Updated to work with jQuery 1.8 changes. Note that the :containsRegex() selector will now need quotes around the regex

Owner

Mottie commented Aug 30, 2012

Updated to work with jQuery 1.8 changes. Note that the :containsRegex() selector will now need quotes around the regex

@hhfa1990

This comment has been minimized.

Show comment Hide comment
@hhfa1990

hhfa1990 Jun 24, 2015

Awesome, Thks!

Awesome, Thks!

@tekhaus

This comment has been minimized.

Show comment Hide comment
@tekhaus

tekhaus Nov 24, 2015

Perfect, just what I needed. Thx!

tekhaus commented Nov 24, 2015

Perfect, just what I needed. Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment