. Advertisement .
..3..
. Advertisement .
..4..
String is one of the data types of JavaScript that is often used by users, on the other hand, string manipulation is extremely popular in programming in general and JavaScript programming in particular.
This article ITtutoria will send you the common string manipulations that Javascript has supported, specifically Check if a String contains a Substring in JavaScript, invite you to follow along.
How to Check If a String contains a Substring in JavaScript
Javascript always gives priority to supporting users who can manipulate and execute commands quickly and conveniently. To check if a String contains a Substring in Javascript, we will summarize some simple and commonly used ways for you below. It will have:
- Use the includes() function
- Use the indexOf() function
- Use the search() function
In addition, in some situations, you can also use test() and match() functions to replace the above functions, it will also return the same result.
Method 1: Use the includes() function
In Javascript, the includes() function in addition to the function is used to be case sensitive, the function is also applied to check whether a string contains a substring or not.
Syntax:
string.includes(searchValue, start).
In there:
- searchValue: is the substring to search for
- start: or position, usually default is 0
If the result is yes, it will display True, otherwise, if it does not, it will display false. For example:
var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
var n = str.includes("ipsum"); // true
Method 2: Use the indexOf() function
In addition, another commonly used function is the indexOf() function. When you want to check if there is a substring in a string, the indexOf() function will do it for you. Besides, the returned result will take the position of the first occurrence of the substring in the main string.
Syntax:
string.indexOf(searchvalue, start)
in there:
- searchValue: is the substring you want to search for
- start: or position, usually defaults to 0.
For example:
var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1); // true
If there is a string that matches the search value, the return result is the first occurrence in the string, counted from 0, otherwise, -1 will be returned.
Method 3: Use the search() function
This method is quite similar to the indexOf() function. The result is the same, if it matches, it will return the string value from the first position, otherwise -1 will be returned. It is different in that, if indexOf() accepts only string input, search() accepts regular expressions as well.
Syntax:
string.search(searchValue)
In there, searchValue: is the substring you want to search for.
For instance:
const str = 'Hello World!';
// string input
str.search('hello'); // output: -1
// using slashes when close string
str.search(/Hello/); // output: 0
// calling the constructor function of the RegExp object
str.search(new RegExp('!')); // output: 11
Method 4: Use the match() function
In addition to the above, in JavaScript, you can also check if a String contains a Substring using match().
Syntax:
str.match(regexp);
regexp is the regular expression string you want to find.
Let’s see the example below:
let str = 'MongoDB, Express, React, Node'
str.match(/MongoDB/)
// ['MongoDB', index: 0, input: 'MongoDB, Express, React, Node', groups: undefined]
str.match(/Java/) // null
str.match(/MongoDB/g) !== null // true
In case the data you enter is in regular form, instead of using the functions mentioned above, match() will be applied more quickly. The result returns an array if it matches the string you are looking for and returns null if it is not found.
Method 5: Use the test() function
This method is not very popular, but it will help you get the same result. The returned result will be in boolean form, if it matches the string to be searched, it will return true, otherwise it will be False.
Syntax:
regexp.test(str);
In there, “str” will take the regular expression as well. For instance:
const str = 'Hello World!';
// using regular expression
/hello/.test(str); // output: false (boolean)
// calling the constructor function of the RegExp object
(new RegExp('Hello')).test(str); // output: true (boolean)
Conclusion
In short, ITtutoria has shown you the easiest and fastest ways to Check if a String contains a Substring in JavaScript. Hope you will find the information useful for you. If you have any questions or concerns, please contact us immediately for support as soon as possible. Thanks for reading!
Leave a comment