Omar
Omar

Reputation: 40182

jQuery .search() to any string

I saw this code snippet:

$("ul li").text().search(new RegExp("sometext", "i"));

and wanted to know if this can be extended to any string?

I want to accomplish the following, but it dosen't work:

$("li").attr("title").search(new RegExp("sometext", "i"));

Also, anyone have a link to the jQuery documentation for this function? I fail at googling apparently.

Upvotes: 10

Views: 118263

Answers (3)

Jacob Relkin
Jacob Relkin

Reputation: 163238

search() is a String method.

You are executing the attr function on every <li> element. You need to invoke each and use the this reference within.

Example:

$('li').each(function() {
    var isFound = $(this).attr('title').search(/string/i);
    //do something based on isFound...
});

Upvotes: 27

Biswajit Roy
Biswajit Roy

Reputation: 81

if (str.toLowerCase().indexOf("yes") >= 0)

Or,

if (/yes/i.test(str))

Upvotes: 8

Chuck Vose
Chuck Vose

Reputation: 4580

Ah, that would be because RegExp is not jQuery. :)

Try this page. jQuery.attr doesn't return a String so that would certainly cause in this regard. Fortunately I believe you can just use .text() to return the String representation.

Something like:

$("li").val("title").search(/sometext/i));

Upvotes: 2

Related Questions