JavaScript Program to Check Whether a String Contains a Substring

Updated on November 7, 2024
Check whether a string contains a substring header image

Introduction

In JavaScript, checking if a string contains a specific substring is a common task that arises in many web development scenarios. This capability is crucial for validating inputs, searching text, and manipulating strings based on certain conditions.

In this article, you will learn how to use JavaScript to determine whether a string includes a particular substring. Explore different methods with examples to see how minor variations adapt to different programming needs.

Using String.prototype.includes()

Basic Example of Checking Substring

  1. Define the main string and the substring you want to check.

  2. Use the includes() method to perform the check.

    javascript
    const mainString = "Hello, welcome to the programming world!";
    const substring = "welcome";
    const isPresent = mainString.includes(substring);
    console.log(isPresent);
    

    This code checks if "welcome" is part of mainString. The includes() method returns true because "welcome" is indeed contained within the main string.

  1. Understanding that includes() is case-sensitive.

  2. Use the same method to check with different casing.

    javascript
    const caseSensitiveCheck = mainString.includes("Welcome");
    console.log(caseSensitiveCheck);
    

    Here, despite "Welcome" being part of the text semantically, the output will be false because includes() is case-sensitive and "W" is uppercase in the query but not in the text.

Using String.prototype.indexOf()

Implementing Substring Check

  1. Utilize the indexOf() method which returns the position of the string if found, or -1 if not.

  2. Check a string for a substring presence based on the method's return value.

    javascript
    const index = mainString.indexOf("welcome");
    const found = index !== -1;
    console.log(found);
    

    In this snippet, indexOf() searches for "welcome" and returns its starting index in the main string, which is then checked against -1. As "welcome" is found, the result is true.

Using Regular Expressions

Matching a Substring with RegExp

  1. Construct a regular expression to match the substring.

  2. Apply the RegExp with test() to determine if the substring exists.

    javascript
    const regex = /welcome/i;  // 'i' for case-insensitive matching
    const regexCheck = regex.test(mainString);
    console.log(regexCheck);
    

    This example uses a regular expression with the i flag for case insensitivity. The test() method returns true, indicating that "welcome" exists in mainString regardless of the text's casing.

Conclusion

JavaScript offers several methods to check whether a string contains a specific substring. Depending on your needs, choose includes() for a straightforward, case-sensitive check, indexOf() to possibly retrieve the substring's position, or regular expressions for more complex matching requirements. By mastering these techniques, you enhance your string manipulation skills, essential for effective JavaScript programming. Keep these methods in mind for efficient string searching and validation in your projects.