Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
In how many ways can we find a substring inside a string in javascript?
JavaScript provides several methods to find a substring inside a string. The most commonly used approaches are indexOf(), includes(), search(), and regular expressions with match().
Using indexOf() Method
Syntax
string.indexOf(substring, startPosition)
The indexOf() method returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. This method is case-sensitive.
Example
<html>
<body>
<script>
var company = "Tutorialspoint";
document.write("Index of 'point': " + company.indexOf('point'));
document.write("<br>");
document.write("Is 'Tutor' present: " + (company.indexOf('Tutor') !== -1));
document.write("<br>");
document.write("Is 'tutor' present: " + (company.indexOf('tutor') !== -1));
</script>
</body>
</html>
Output
Index of 'point': 9 Is 'Tutor' present: true Is 'tutor' present: false
Using includes() Method (ES6)
Syntax
string.includes(substring, startPosition)
The includes() method returns true if the substring is found, and false otherwise. This method is also case-sensitive and provides a cleaner way to check substring existence.
Example
<html>
<body>
<script>
var company = "tutorialspoint";
document.write("Contains 'point': " + company.includes('point'));
document.write("<br>");
document.write("Contains 'Point': " + company.includes('Point'));
document.write("<br>");
document.write("Contains 'tutorial': " + company.includes('tutorial'));
</script>
</body>
</html>
Output
Contains 'point': true Contains 'Point': false Contains 'tutorial': true
Using search() Method
The search() method searches for a match using regular expressions and returns the index of the first match or -1 if not found.
Example
<html>
<body>
<script>
var text = "JavaScript is awesome";
document.write("Search 'Script': " + text.search('Script'));
document.write("<br>");
document.write("Search case-insensitive: " + text.search(/script/i));
</script>
</body>
</html>
Output
Search 'Script': 4 Search case-insensitive: 4
Using match() with Regular Expressions
The match() method retrieves the result of matching a string against a regular expression, providing more flexibility for complex pattern matching.
Example
<html>
<body>
<script>
var sentence = "Learning JavaScript programming";
var result = sentence.match(/script/i);
document.write("Match result: " + (result !== null));
document.write("<br>");
document.write("Found at index: " + (result ? result.index : "Not found"));
</script>
</body>
</html>
Output
Match result: true Found at index: 12
Comparison of Methods
| Method | Returns | Case Sensitive | Regular Expressions |
|---|---|---|---|
indexOf() |
Index or -1 | Yes | No |
includes() |
Boolean | Yes | No |
search() |
Index or -1 | Depends on regex | Yes |
match() |
Array or null | Depends on regex | Yes |
Conclusion
Use includes() for simple boolean checks, indexOf() when you need the position, and search() or match() for pattern-based searching with regular expressions. Each method serves different use cases in substring detection.
