Being fairly new to JavaScript, I'm unable to discern when to use each of these.
Can anyone help clarify this for me?
Being fairly new to JavaScript, I'm unable to discern when to use each of these.
Can anyone help clarify this for me?
If your situation requires the use of a regular expression, use the search()
method, otherwise; the indexOf()
method is more performant.
search
will evaluate a string into a regex even if you don't want to. –
Advertisement "hello.".search(".")
- it returns 0, not 5 because .
is the regex token for "any character" –
Brigid 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)
search value can be regular expression
str.search('book') // 8
str.search(/book/i) // 0 ( /i =case-insensitive (Book == book)
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.
I think the main difference is that search accept regular expressions.
Check this reference:
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.:
Search() - accepts both string literals or string objects and regular expressions. But it doesn't accepts a index to start the search from.
"baby/e/lephant".indexOf(m);
? –
Hygro Search finds it's matches with a regular expression, but has no offsets. IndexOf uses literals to match, but has an offset.
Besides all the other considerations already mentioned indexOf is faster by a very large margin, if you don't need to do funky regex and are just doing a simple find then do not use search. robisrob asked for some evidence so I whipped up a quick test.
Starting...
IndexOf test - loop count 10000000
IndexOf test end - duration: 4.600000023841858ms
Search test - loop count 10000000
Search test end - duration: 1221.6999999284744ms
All Done
That is a significant difference, only use search if you are going to be using regular expressions.
I can post the test code if anyone is interested.
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>
search
converts a string into a RegExp
, so for instance, str.search("d........e");
will also match at character 39. –
Snatch © 2022 - 2024 — McMap. All rights reserved.