Asked : Nov 17
Viewed : 22 times
Usually, I would expect a String.contains()
method, but there doesn't seem to be one.
What is a reasonable way to check for this?
Nov 17
ECMAScript 6 introduced String.prototype.includes
:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
includes
doesn’t have Internet Explorer support, though. In ECMAScript 5 or older environments, use String.prototype.indexOf
, which returns -1 when a substring cannot be found:
var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1); // true
answered Dec 23
Checking if a string contains a substring is a common task in any programming language. For instance, say you are building an online game. You may want to check whether a username contains a banned phrase to make sure all usernames are suitable for your game.
1. includes().
The first method is to check if a JavaScript string contains a character or phrase using the includes() method. includes() method is the most common method for checking if a string contains a letter or series of letters, and was designed specifically for that purpose, means JavaScript string contains substring or not.
In JavaScript, includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.
The syntax for the includes() method is:
string.includes(word);
Here is an example for the includes() method,
let example = "Lurem ipsum is dummy text";
let ourSubstring = "ipsum";
if (example.includes(ourSubstring)) {
console.log("The word ipsum is in the string.");
} else {
console.log("The word ipsum is not in the string.");
}
Note: The includes() method is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.
2. indexOf().
The second method is to check if a JavaScript string contains a character or phrase using the indexOf() method.
The JavaScript indexOf() method, like includes(), checks if a string includes another string. What is different is the output from these two functions.
The syntax for the indexOf() method is:
string.indexOf(word);
Here is an example for the indexOf() method,
let example = "Lurem ipsum is dummy text";
let ourSubstring = "ipsum";
if (example.indexOf(ourSubstring) != 0) {
console.log("The word ipsum is in the string.");
} else {
console.log("The word ipsum is not in the string.");
}
3. Using regular expression Regex.
The third and very interesting method is to check whether the javaScript string contains a substring or not.
let str = "Example String!";
/Example/.test(str);
answered Dec 23
There are three methods for checking if a JavaScript string contains another character or sequence of characters:
1. includes().
Syntax
string.includes(word); // return true or false
Example
let example = "Example String!";
let ourSubstring = "Example";
if (example.includes(ourSubstring)) { // return true or false
console.log("The word Example is in the string.");
} else {
console.log("The word Example is not in the string.");
}
2. indexOf().
Syntax
string.indexOf(word); // return -1
Example
let example = "Example String!";
let ourSubstring = "Example";
if (example.indexOf(ourSubstring) != 0) {
console.log("The word Example is in the string.");
} else {
console.log("The word Example is not in the string.");
}
3. Regular expressions (regex).
Example
let str = "Example String!";
/Example/.test(str);
we discussed the basics of strings in JavaScript. After that, we discussed three ways in which you can check if a string contains a substring in JavaScript: using includes(), indexOf(), and regex.
answered Dec 23