In this blog, we know the difference between search() and indexOf() in javascript. We also know which one is faster and reliable to use.
First, we discuss
Search() Method
Description
The search() method is used to search for a match between a regular expression and a specified string. If a match is found, search() method returns the index of the regular expression inside the string, otherwise, it returns -1.
Syntax
Its syntax is as follows −
[pastacode manual=”string.search(regexp)%3B%0D%0A” provider=”manual” lang=”php”/]
Argument Details
regexp − A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).
Example
[pastacode manual=”%3Chtml%3E%20%0D%0A%3Chead%3E%20%0D%0A%3Ctitle%3EJavaScript%20String%20search()%20Method%3C%2Ftitle%3E%20%0D%0A%3C%2Fhead%3E%20%0D%0A%3Cbody%3E%20%0D%0A%3Cscript%20type%20%3D%20%22text%2Fjavascript%22%3E%20%0D%0Avar%20str%20%3D%20%22friends%20say%20Hello%22%3B%20%0D%0Avar%20substr%20%3D%20%22Hello%22%3B%20%0D%0Avar%20index%20%3D%20str.search(substr)%3B%20%0D%0Aif(index!%3D-1)%20%7B%20%0D%0Adocument.write(substr%20%2B%20%22%20found%20at%20%22%20%2B%20index%20%2B%20%22%20position.%3Cbr%3E%22)%3B%20%0D%0A%7D%20%0D%0Aelse%20%7B%20%0D%0Adocument.write(substr%20%2B%20%22%20does%20not%20exist%20in%20the%20%22%20%2B%20str%20%2B%20%22.%3Cbr%3E%22)%3B%20%0D%0A%7D%20%0D%0A%3C%2Fscript%3E%20%0D%0A%3C%2Fbody%3E%20%0D%0A%3C%2Fhtml%3E” provider=”manual” lang=”default”/]
Output
[pastacode manual=”Hello%20found%20at%2012%20position.” provider=”manual” lang=”php”/]
Supported Browser
Internet Explorer 7 | Firefox 3.6 | Google Chrome 7 | Safari 5.0.1 | Opera 10 |
Yes | Yes | Yes | Yes | Yes |
You May Also Like:
- Limit number of login attempt using PHP & MySQL
- Take Image Snapshot from a webcam with Jquery and HTML
- How to decode JSON data and accessing the results in PHP
IndexOf() Method
Description
The indexOf() method returns the index within the calling string object of the first occurrence of the specified value, starting the search at startIndex. It returns -1 if the value is not found.
Syntax
Use the following syntax to use the indexOf() method.
[pastacode manual=”string.indexOf(searchValue%5B%2C%20fromIndex%5D)” provider=”manual” lang=”php”/]
Argument Details
- searchValue − A string representing the value to search for.
- fromIndex − The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.
Example
[pastacode manual=”%3Chtml%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml%22%20xml%3Alang%3D%22en%22%20lang%3D%22en%22%3E%20%0D%0A%3Chead%3E%20%0D%0A%3Ctitle%3EJavaScript%20String%20IndexOf()%20Method%3C%2Ftitle%3E%20%0D%0A%3C%2Fhead%3E%20%0D%0A%3Cbody%3E%20%0D%0A%3Cscript%20type%3D%22text%2Fjavascript%22%3E%20%0D%0Avar%20Str%3D%22Brave%20new%20world%22%3B%20%0D%0Adocument.write(%22The%20index%20of%20the%20first%20w%20from%20the%20beginning%20is%20%22%20%2B%20Str.indexOf(%22w%22)%2B%22%3Cbr%20%2F%3E%22)%3B%20%0D%0Adocument.write(%22The%20index%20of%20’new’%20from%20the%20beginning%20is%20%22%20%2B%20Str.indexOf(%22new%22))%3B%20%0D%0A%3C%2Fscript%3E%20%0D%0A%3C%2Fbody%3E%20%0D%0A%3C%2Fhtml%3E” provider=”manual” lang=”default”/]
Output
[pastacode manual=”The%20index%20of%20the%20first%20w%20from%20the%20beginning%20is%208%0D%0AThe%20index%20of%20’new’%20from%20the%20beginning%20is%206″ provider=”manual” lang=”php”/]
Supported Browser
Internet Explorer 7 | Firefox 3.6 | Google Chrome 7 | Safari 5.0.1 | Opera 10 |
Yes | Yes | Yes | Yes | Yes |
Though indexOf() and search() both methods are used to check whether a substring exists in the string or not and returns either of the index of the substring or -1 (if substring does not exist in the string), there is a difference between indexOf() and search() methods in javascript.
Now, you can clearly see that in indexOf() method, there is an optional parameter(offset) from where we can start the searching but the method search() does not have this feature. It simply takes the substring and starts searching from the 0th index.
Thanks for Reading, Keep Visiting.