views:

2358

answers:

3

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.

+7  A: 

search() is a String method.

You are executing the attr function on EVERY li element. You need to call each and use this inside the function.

Example:

$( 'li' ).each( function() {
   var isFound = $( this ).attr( 'title' ).toString().search( new RegExp( /string/i ) );
} );
Jacob Relkin
+1  A: 

Here is some documentation for the search method. It's part of Javascript, not JQuery.

http://www.w3schools.com/jsref/jsref_search.asp

Robert Harvey
A: 

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));
Chuck Vose