JavaScript String search() Method

You are Here:

JavaScript String search() Method

The search() method searches a string for a specified value, and returns the position (index value) of the match.

Using String

The following example uses string as a value to be searched.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var str = "Tiger is king of the jungle"; var result = str.search("is"); document.write(result); </script> </body> </html>

Syntax

str.search(searchValue)

Parameter Values

ValueTypeExplanation
searchValueRequiredA value to be searched within this string.
Use string or regexp to search a value within this string.

Return Values

ValueExplanation
0 to array.lengthA index of a searchValue within the string.
-1When the searchValue is not found within the string.

Using Regexp

The following example uses Regexp as a value to be searched.

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var str = "Tiger is king of the jungle"; var result = str.search(/is/gi); document.write(result); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author