Check if String Contains a Specific Word or Substring in JavaScript

Author — Nitish Kumar

Let’s say you have a string and you want to check if it contains a specific word, character or substring. In this article, you will learn about different ways in which you can perform the check using JavaScript. Here are a few variables that we are going to use:

JavaScript

var theString = "I have been looking for Sam.";
var theWord  = "look";
var theCharacter = "I";
var theSubstring = "for Sam";
On This Page

Using String.indexOf() to Check if String Contains Substring

The fastest way to check if a string contains a particular word or substring is with the help of String.indexOf() method. This method returns the index of the first occurrence of the specified substring inside the calling String object. If the substring or word is not found, it returns -1.

This means that you can compare the value returned by the indexOf() method to see if it is equal to -1. If the value is not -1, the calling string contains the substring we are looking for.

JavaScript

var theString = "I have been looking for Sam.";
var theWord  = "looking";
var theCharacter = "I";
var theSubstring = "for Sam";


// Output — The word "looking" exists in given string.
if (theString.indexOf(theWord) !== -1) {
  console.log('The word "' + theWord + '" exists in given string.');
}

// Output — The character "I" exists in given string.
if (theString.indexOf(theCharacter) !== -1) {
  console.log('The character "' + theCharacter + '" exists in given string.');
}

// Output — The substring "for Sam" exists in given string.
if (theString.indexOf(theSubstring) !== -1) {
  console.log('The substring "' + theSubstring + '" exists in given string.');
}

You are not limited to the strict inequality operator (!==), you can also use > -1. This is because if the word or substring exists in the given string, the index returned would always be greater than or equal to 0. However, remember that the greater than operator (>) is slower than the strict inequality operator (!==).

One important point that should be kept in mind is that if you meant to search exactly for “look” in the above string, the method would still return an index greater than -1 because “looking” exists in the string. If you are looking for exact matches, you will have to be extra careful.

This method is case-sensitive so you would have got -1 if you searched for “Looking” instead of “looking”.

Using String.includes() to Check if String Contains Substring

You can also use the String.includes() to check if a string contains another word, character or substring. This method will return TRUE if the substring can be found within the main string and FALSE otherwise.

JavaScript

var theString = "I have been looking for Sam.";
var theWord  = "looking";
var theCharacter = "I";
var theSubstring = "for Sam";

// Output — The word "looking" exists in given string.
if (theString.includes(theWord)) {
  console.log('The word "' + theWord + '" exists in given string.');
}

// Output — The character "I" exists in given string.
if (theString.includes(theCharacter)) {
  console.log('The character "' + theCharacter + '" exists in given string.');
}

// Output — The substring "for Sam" exists in given string.
if (theString.includes(theSubstring)) {
  console.log('The substring "' + theSubstring + '" exists in given string.');
}

Just like String.indexOf(), this method is also case-sensitive. One major issue with this method is that the browser support for String.includes() is not as good as String.indexOf(). If you don’t care about the browser support for Internet Explorer, you can use String.includes() without second thought.

Using String.search() to Check if String Contains Substring

String.search() is another method that you can use to check if a string contains another word, substring or character inside the main string. Unlike String.indexOf() and String.include() that we have covered so far, String.search() accepts a regular expression as its parameter. This means that you can also use it to search for complex patterns instead of basic strings.

String.search() returns the index of first match between the regular expression and given string. If no match is found it returns -1. You can compare the return value of String.search() with -1 to see if the given string has the substring you are looking for.

JavaScript

var theString = "I have been looking for Sam.";
var theWord = "looking";
var theCharacter = "I";
var theSubstring = "for Sam";
var theWordExp  = /looking/g;
var theCharacterExp = /I/g;
var theSubstringExp = /for Sam/g;

// Output — The word "looking" exists in given string.
if (theString.search(theWordExp) !== -1) {
  console.log('The word "' + theWord + '" exists in given string.');
}

// Output — The character "I" exists in given string.
if (theString.search(theCharacterExp) !== -1) {
  console.log('The character "' + theCharacter + '" exists in given string.');
}

// Output — The substring "for Sam" exists in given string.
if (theString.search(theSubstringExp) !== -1) {
  console.log('The substring "' + theSubstring + '" exists in given string.');
}

If you are only looking for basic strings, I would suggest that you use either String.includes() or String.indexOf() instead of String.seach().

Using String.match() to Check if String Contains Substring

The String.match() method is used to retrieve all the matches of a regular expression inside the main string. You can use it to check the existence of another word, character or substring inside the main string by passing them as regular expressions. This method will return null if no matches are found.

JavaScript

var theString = "I have been looking for Sam.";
var theWord = "looking";
var theCharacter = "I";
var theSubstring = "for Sam";
var theWordExp  = /looking/g;
var theCharacterExp = /I/g;
var theSubstringExp = /for Sam/g;

// Output — The word "looking" exists in given string.
if (theString.match(theWordExp) !== null) {
  console.log('The word "' + theWord + '" exists in given string.');
}

// Output — The character "I" exists in given string.
if (theString.match(theCharacterExp) !== null) {
  console.log('The character "' + theCharacter + '" exists in given string.');
}

// Output — The substring "for Sam" exists in given string.
if (theString.match(theSubstringExp) !== null) {
  console.log('The substring "' + theSubstring + '" exists in given string.');
}

Actually, you can use String.match() to get a lot more information than just check the existence of a substring. If you include the g flag in the regular expression, it returns the whole array of matched substrings in case of a match. Using it to check if a string has another substring might be overkill. Also, this method is slow compared to other substring checking methods.

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. You can use four different methods to check if a string contains another word or substring. These methods are: indexOf(), includes(), search() and match().
  2. The indexOf() method will return the index of the matched string while includes() just returns a Boolean TRUE or FALSE. You can use them both without worrying too much about performance. Both these methods are case-sensitive.
  3. If you are planning to perform case-insensitive searches, using search() with the case-insensitive flag i might be a better idea. However, you will have to escape some special characters for the search to be successful. Using the search() method also gives you more flexibility over simple string matching methods indexOf() and includes().
  4. You can also use match() to see if a string has another word or substring. However, if you just want to know if a match exists, using it might be an overkill.

Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to check if a string contains another word, character or substring in JavaScript.

Rate this post —

Very PoorPoorAverageGoodExcellent (1 votes, average: 5.00 out of 5)
Loading...
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0%