Use test() whenever you want to know whether a pattern is found in a string (similar to the String.prototype.search() method); for more information (but slower execution) use the exec() method (similar to the String.prototype.match() method). As with exec() (or in combination with it), test() called multiple times on the same global regular expression instance will advance past the previous match.
Returns: True (0) or False (-1), and also a can search the entire string or can start from a point in the string using the fromIndex parameter.
in indexOf Found == 0
in indexOf not Found == -1
FOUND indexOf !== -1
FOUND indexOf > -1if you want to say something is found != -1 is a safer equivalent because 0 could as be a NULL value
You can also say if Something is found it is > -1 because a found string is equal to 0 and that is greater than -1
Returns: search()returns the index of the first match of the regular expression inside the string. Otherwise, it returns -1
MDN definition of search()
Returns array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
John began working in/on/with the web when he graduated from the University of Kentucky in 2002 (and also when being a Rock star fell through.) Throughout his career he has built lots of websites, both great and small. He runs a web development company, 3rd Dimension Design and also is a web developer for really big higher ed institution. When he isn’t working, he’s probably playing with his kid, watching baseball or working on personal projects. His least favorite thing to do is write bios about himself (like now). He’s really cool and super attractive. You can find his blog at JohnHarbison.net or follow him on Twitter @Harbison