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
Value | Type | Explanation |
---|---|---|
searchValue | Required | A value to be searched within this string. Use string or regexp to search a value within this string. |
Return Values
Value | Explanation |
---|---|
0 to array.length | A index of a searchValue within the string. |
-1 | When 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.