Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to check if string contains substring in JavaScript

    How to check if string contains substring in JavaScript

    RahulBy RahulNovember 28, 20142 Mins ReadUpdated:May 10, 2020

    The JavaScript indexOf() method search for the substring in string and returns the position of the first occurrence of a specified substring. If substring not found, it returns -1. So you can use JavaScript IndexOf() method to check if a string contains substring in it. In this tutorial we will also provides example to use JavaScript includes() and search() methods to find substring in another string.

    In a comparison of all 3 methods describes below, indexOf() can be better due to its speed of execution. If no speed matters, you can use any of the following methods.

    Using indexOf() Method

    JavaScript indexOf() method will return the starting position number substring in the main string. If substring not found, it will return -1 as a result.

    1
    2
    3
    4
    var str = "Hello world";
    var substr = "World";
    var result = str.indexOf(substr) > -1;
    alert(result);

    Result will be as below:

     true 
    
    • How to Append Element to Array in JavaScript

    2. Using includes() Method:

    Also you could use JavaScript includes() method. It returns true on match found and false if no match found. Use below sample code to test this:

    1
    2
    3
    4
    var str = "Hello world";
    var substr = "World";
    var result = str.includes(substr) > -1;
    alert(result);

    Result will be as below:

     true 
    

    3: Using search() Method

    You could also use the JavaScript search() method. It returns string position number if match found. If not match found returns -1.

    1
    2
    3
    4
    var str = "Hello World";
    var expr = "/World/";
    var result = str.search(expr) > -1;
    alert(result);

    Result will be as below:

     true 
    
    • >How to Remove Array Element by Value in JavaScript
    inclues indexof javastring position search string substring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Concatenate two string variables in Bash Script
    Next Article How to Redirect User to Another Webpage with JavaScript

    Related Posts

    What is difference between var, let and const in JavaScript?

    3 Mins Read

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    Shell Script – Remove Double Quote (“”) from a String

    1 Min Read

    Remove First Character from String in JavaScript

    1 Min Read

    How to Search files with case-insensitive names in Linux

    Updated:December 13, 20202 Mins Read

    3 Comments

    1. Rey on October 8, 2019 1:08 am

      also var expr = “/World/”; is written incorrectly.

      it should have been written var expr = /World/; to achieve the expected results

      Reply
    2. Rey on October 8, 2019 12:54 am

      var result = str.includes(substr) > -1; is written wrong… will return also true when substr is not in str, as false > -1 evaluates to true!!

      should have been written
      var result = str.includes(substr); // result will return true if substr in str, and will return false if not

      Reply
    3. John on February 10, 2016 9:53 am

      You can also very easily do this by using this code, implemented in modernizr lib:

      function contains(str, substr) {
      return !!~(” + str).indexOf(substr);
      }

      U can see the full text, docs, usage examples and more at cocycles:

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.