In JavaScript strings, users can check a specific character, word, or substring in the parent string. Finding strings containing substrings is a typical task in any programming language.
Different methods allow programmers to efficiently and easily find any substring or check whether the string contains any specific word. Some of these are Regex match(), search(regExp), include(), and indexOf() methods for finding a specified value in the string.
Let us dive deeper into how to find whole words in a string using the two popular JavaScript methods, Regex match() and search(regExp).
Following are the methods we are using to check whether the string contains a specific word using JavaScript:
Method 1: The JavaScript Regex match() method to check substrings
Users can use the Regex match() method to match a string to the given regular expression. The match() method returns null if it cannot find any matches. Here we are using the method to determine Case sensitivity and Case insensitivity:
Code:
<html>
<head>
<title>Check whether a string contains a specific word using JavaScript</title>
</head>
<body>
<h2> Example of JavaScript match() method </h2>
<p><strong>String</strong>: Here, we write an example of JavaScript string</p>
<p><strong>Word</strong>: JavaScript</p>
<p id = "demo"></p>
<script>
var a = "Here, we write an example of JavaScript string"
// The method will check and find whether the specific word is present in the string
if(a.match(/(^|\W)JavaScript($|\W)/)){
document.getElementById('demo').innerHTML = 'The word matched';
}else {
document.getElementById('demo').innerHTML = 'The word not matched or found';
}
</script>
</body>
</html>
Output:
Explanation:
In the above code, we used the match() method to find whether any specific word is present in the parent string. We used regular expressions with symbols to search for the word and match it.
Case Insensitive Example
<html>
<head>
<title>Check whether a string contains a specific word using JavaScript</title>
</head>
<body>
<h2> Example of JavaScript match() method </h2>
<p><strong>String</strong>: Here, we write an example of JavaScript string</p>
<p><strong>Word</strong>: javascript</p>
<p id = "demo"></p>
<script>
var a = "Here, we write an example of JavaScript string"
// The method will check and find whether the specific word is present in the string
if(a.match(/(^|\W)javascript($|\W)/i)) {
document.getElementById('demo').innerHTML = 'The word matched';
}else {
document.getElementById('demo').innerHTML = 'The word not matched or found';
}
</script>
</body>
</html>
Output:
Explanation:
Here, we only added another symbol, i.e., "i" in the regular expression, which returned "The word not matched or found."
Method 2: The JavaScript search(regExp) method to check substrings
Users can also use the regular expressions with the string methods search() to check substring in JavaScript.
<html>
<head>
<title>Check whether a string contains a specific word using JavaScript</title>
</head>
<body>
<h2> Example of JavaScript search() method </h2>
<p><strong>String</strong>: Here, we write an example of JavaScript string</p>
<p><strong>Word</strong>: JavaScript</p>
<p id = "demo"></p>
<script>
var a = "Here, we write an example of JavaScript string"
// The method will check and find whether the specific word is present in the string
if(a.search(/(^|\W)JavaScript($|\W)/)) {
document.getElementById('demo').innerHTML = 'The word matched';
}else {
document.getElementById('demo').innerHTML = 'The word not matched or found';
}
</script>
</body>
</html>
Output:
Explanation:
The search() method runs a search for a match between the given regular expression and the specified string input. It takes the regular expression and searches for the word by matching it.
Case Insensitive Example
<html>
<head>
<meta charset="utf-8">
<title>Check whether a string contains a specific word using JavaScript</title>
</head>
<body>
<h2> Example of JavaScript search() method </h2>
<p><strong>String</strong>: Here, we write an example of JavaScript string</p>
<p><strong>Word</strong>: javascript</p>
<p id = "demo"></p>
<script>
var a = "Here, we write an example of JavaScript string"
// The method will check and find whether the specific word is present in the string
if(a.search(/(^|\W)javascript($|\W)/i)) {
document.getElementById('demo').innerHTML = 'The word matched';
}else {
document.getElementById('demo').innerHTML = 'The word not matched or found';
}
</script>
</body>
</html>
Output:
Explanation:
In the above case insensitive example, we only added another symbol, i.e., "i" in the regular expression, which returned "The word not matched or found."
Conclusion
In this article, we matched the specified string with the parent string to find whether the JS string has the word or character. But when users want to find a whole word, they cannot use the indexOf() or includes() methods because they cannot search or check the complete word in the parent string.