The JavaScript indexOf() method search for the substring in string and returns the position of the first occurrence of a specified substring. If substring not found, it returns -1. So you can use JavaScript IndexOf() method to check if a string contains substring in it.
How do you check if a string is a substring in JavaScript?
String Contains JavaScript: Regex
We can use the RegExp. test() method to check whether a string contains a substring. Here's an example: let str = "Example String!"; /Example/.
How can use substring in JavaScript with example?
The JavaScript String.prototype.substring() returns the part of the string between the start and end indexes:
- str.substring(startIndex [, endIndex]) ...
- let str = 'JavaScript Substring'; let substring = str.substring(0,10); console.log(substring); ...
- JavaScript.