oops, copy/paste error. as there is no "text" method on RegExp
objects, disregard the snippet above. it should be:

// Existing "contains" filter
function (a, i, m) {
    return (a.textContent || a.innerText || jQuery(a).text() ||
"").indexOf(m[3]) >= 0;
}

// New ":text-equals" filter
function (a, i, m) {
    return new RegExp("^" + m[3] + "$").test(a.textContent ||
a.innerText || jQuery(a).text() || "");
}


On Oct 6, 9:50 am, KeeganWatkins <keeganwatk...@gmail.com> wrote:
> Hi Julijan,
>
> It sounds like what you need is a ":text-equals" filter The reason
> ":contains" fails in this case is because it does a global match. By
> anchoring the same RegExp against the beginning and end of the string,
> you should be able to select elements whose text matches exactly:
>
> // Existing "contains" filter
> function (a, i, m) {
>     return (a.textContent || a.innerText || jQuery(a).text() ||
> "").indexOf(m[3]) >= 0;
>
> }
>
> // New ":text-equals" filter
> function (a, i, m) {
>     return new RegExp("^" + m[3] + "$").text(a.textContent ||
> a.innerText || jQuery(a).text() || "");
>
> }
>
> NOTE: I haven't tested this code, but the idea itself is simple...
> instead of searching for a given string anywhere in the element's text
> content, anchor the RegExp to begin searching at the beginning of the
> element's text and match exactly through the end of the element's
> text. Hope that helps.
>
> On Oct 5, 4:20 am, "ryan.j" <ryan.joyce...@googlemail.com> wrote:
>
> > sounds like you want to .filter() the results, but that will still
> > iterate. the thing is anything that grabs a bunch of elements and
> > picks certain ones out is going to loop through them at some point.
>
> > if you wanted to do it entirely via selectors (and depending on
> > whether there is a appropriate attribute for that sort of thing in the
> > element you select) i guess you could use [attribute=xyz].
>
> > On Oct 5, 9:20 am, Julijan Andjelic <julijan.andje...@gmail.com>
> > wrote:
>
> > > Is there a way (selector) that would allow me to select an element by
> > > exact mach of containing text.
>
> > > example:
>
> > > <div>
> > >     This is some text
> > > </div>
>
> > > <div>
> > >     This
> > > </div>
>
> > > Lets say i want to select only the second div
> > > $("div:contains('This')")...
> > > okay, it would select it, but...it would also select the first one.
> > > I could iterate through each div and check if the text is matching but
> > > that's kinda dirty way of doing this. Is there any selector which
> > > would allow me to do this without iteration?

Reply via email to