views:

495

answers:

5

Being fairly new to Javascript, I'm unable to discern when to use each of these.

Can anyone help clarify this for me?

+2  A: 

indexOf is for plain substrings, search can do regular expressions.

Greg
+9  A: 

If you require a regular expression, use search(). Otherwise, indexof() is going to be faster.

ng.mangine
+2  A: 

I think the main difference is that search accept regular expressions.

Check this reference:

CMS
+2  A: 

The search function (one description here) takes a regular expression, which allows you to match against more sophisticated patters, case-insensitive strings, etc., while indexOf (one description here) simply matches a literal string. However, indexOf also allows you to specify a beginning index.

joel.neely
A: 

Search finds it's matches with a regular expression, but has no offsets. IndexOf uses literals to match, but has an offset.

IndexOf

Search

Jason Lepack