What is the difference between indexOf() and search()?

JavascriptString

Javascript Problem Overview


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

Can anyone help clarify this for me?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

indexOf() and search()

  • common in both

    i) return the first occurrence of searched value

    ii) return -1 if no match found

     let str='Book is booked for delivery'
     str.indexOf('b')   // returns position 8
     str.search('b')    // returns position 8 
    

  • special in indexOf()

    i) you can give starting search position as a second argument

     str.indexOf('k')   // 3
     str.indexOf('k',4) // 11 (it start search from 4th position) 
    

  • special in search()

search value can be regular expression

str.search('book') // 8
str.search(/book/i)  // 0   ( /i =case-insensitive   (Book == book)

reference

Solution 5 - Javascript

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

Check this reference:

Solution 6 - Javascript

IndexOf() - it accepts string literals or string objects but not regular expressions. It also accepts a zero-based integer value to start its search from, e.g.:

  1. "babyelephant".indexOf("e"); // gives you 4
  2. "babyelephant".indexOf("e",5); // gives you 6 as the search starts from 6th position or 5th index.
  3. var m= /e/; "babyelephant".indexOf(m); //gives -1 as it doesnt accepts regular expressions.

Search() - accepts both string literals or string objects and regular expressions. But it doesn't accepts a index to start the search from.

Solution 7 - Javascript

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

IndexOf

Search

Solution 8 - Javascript

Without a regex, there is no practical difference between indexOf and search.

The below example gives a live demo:

function FromSearch() {

  var str = document.getElementById("demo").innerText;
  var n = str.search("difference");
  document.getElementById("Location").innerHTML = n;
}

function FromindexOf() {
  var str = document.getElementById("demo").innerText;
  var n = str.indexOf("difference");
  document.getElementById("Location").innerHTML = n;
}

<p id="demo">Without a <a href='http://www.w3schools.com/js/js_regexp.asp'>regex</a>, there is no practical difference between <a href='http://www.w3schools.com/jsref/jsref_indexof.asp'>indexOf</a> and <a href='http://www.w3schools.com/jsref/jsref_search.asp'>search</a>
</p>

<button onclick="FromSearch()">From search</button>

<button onclick="FromindexOf()">From indexOf</button>

<p>Location of difference in the above sentence is:</p>

<mark id="Location"></mark>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionColin BrockView Question on Stackoverflow
Solution 1 - Javascriptng.mangineView Answer on Stackoverflow
Solution 2 - JavascriptGregView Answer on Stackoverflow
Solution 3 - Javascriptjoel.neelyView Answer on Stackoverflow
Solution 4 - JavascriptK23rajView Answer on Stackoverflow
Solution 5 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 6 - JavascriptbablueView Answer on Stackoverflow
Solution 7 - JavascriptLeppyR64View Answer on Stackoverflow
Solution 8 - JavascriptxameeramirView Answer on Stackoverflow