jQuery :contains(regex)?
23,002
Try ,
var regex = new RegExp("[0-9]"); // expression here
$("#id a").filter(function () {
return regex.test($(this).text());
});
Author by
Edge
Updated on April 08, 2020Comments
-
Edge about 4 years
Is it possible to have a jQuery selector where the :contains() is a regular expression?
I need to select an element where the innerHTML contains something findable through regex?
I know it's a short question. My apologies.
var t = $("#id a:containsRegex("/[0-9]/g")"); // Doesn't work
-
zerkms about 10 yearsUse
.filter()
-
-
Tomalak about 10 years@Pilot using
innerText
is wrong. Use$(this).text()
instead. -
Deepak Ingole about 10 years@Tomalak Updated..
innerText
is only valid for block elements ? M I right Master -
ThiefMaster about 10 yearsfyi you can use
var regex = /[0-9]/;
to make it more readable -
Tomalak about 10 years@Pilot No,
innerText
is simply a proprietary DOM extension once invented by Microsoft. It should not be used as the default way to get text from an element, all modern browsers support the W3C standardtextContent
property. -
Deepak Ingole about 10 years@Tomalak thanks for such a valuable comment..Much appreciated.
-
Muhammad Umer about 9 yearsjust for others' reference
filter
is a jquery function not a native array filter function which i was mistaking it for.