jQuery String Contains Manipulation? jQuery String Contains Manipulation? jquery jquery

jQuery String Contains Manipulation?


This is done with indexOf, however it returns -1 instead of False if not found.

Syntax
string.indexOf(searchValue[, fromIndex])

Parameters
searchValue - A string representing the value to search for.
fromIndex - The location within string to start the search from. It can be any integer between 0 and the length of string. The default value is 0.

Return
The first index in string at which the start of the substring can be found, or -1 if string does not contain any instances of the substring.


As Paolo and cletus said, you can do it using indexOf().

Valid to mention is that it is a javascript function, not a jQuery one.

If you want a jQuery function to do this you can use it:

jQuery.fn.contains = function(txt) { return jQuery(this).indexOf(txt) >= 0; }


The indexOf operator works for simple strings. If you need something more complicated, it's worth pointing out that Javascript supports regular expressions.