JQuery.search () for any string

I saw this piece of code:

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

and wanted to know if this could be extended to any line?

I want to do the following, but it does not work:

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

Also, who has a link to jQuery documentation for this function? I don't seem to understand.

+9
javascript jquery string
Dec 06 '09 at 6:00
source share
3 answers

search() is a String method.

You execute the attr function on the element each <li> . You need to call each and use the this link inside.

Example:

 $('li').each(function() { var isFound = $(this).attr('title').search(/string/i); //do something based on isFound... }); 
+25
Dec 06 '09 at 6:12
source share
 if (str.toLowerCase().indexOf("yes") >= 0) 

Or

 if (/yes/i.test(str)) 
+6
Jun 28 2018-12-12T00:
source share

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

Try this page . jQuery.attr does not return a string, so that would be inevitable in this regard. Fortunately, I believe that you can simply use .text() to return a String representation.

Something like:

 $("li").val("title").search(/sometext/i)); 
+2
Dec 06 '09 at 6:18
source share



All Articles