Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to check a string is entirely made up of the same substring in JavaScript ?

  • Last Updated : 10 Jun, 2019

Given a string and the task is to determine whether the string is made up of the same substrings or not.

Approach:

  • Initialize a string to the variable.
  • Use test() method to test a pattern in a string.
  • The test() method returns true if match is found, otherwise it returns false.

Example 1: This example checks for string geeksgeeksgeeks which is made up of same substring, so it returns True.




<!DOCTYPE HTML> 
<html
    <head
        <title
            How to check a string is entirely
            made of same substring ?
        </title>
    </head
      
    <body style = "text-align:center;"
  
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1
  
        <p id = "GFG_UP" style =
            "font-size: 16px; font-weight: bold;">     
        </p>
          
        <button onclick = "gfg_Run()"
            Click here
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
          
            var str = "geeksgeeksgeeks";
          
            el_up.innerHTML = "Str = '" + str + "'";
            function check(str) {
                return /^(.+)\1+$/.test(str)
            }
              
            function gfg_Run() {
                ans = "String is not made up of same substrings";
                if (check(str)) {
                    ans = "String is made up of same substrings"; 
                }
                el_down.innerHTML = ans;
            }         
        </script
    </body
</html>                    

Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example checks for string geeksgeekgeeks which is not made up of same substrings so it returns False.




<!DOCTYPE HTML> 
<html
    <head
        <title
            How to check a string is entirely
            made of same substring ?
        </title>
    </head
      
    <body style = "text-align:center;"
  
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1
  
        <p id = "GFG_UP" style =
            "font-size: 16px; font-weight: bold;">     
        </p>
          
        <button onclick = "gfg_Run()"
            Click here
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
          
            var str = "geeksgeekgeeks";
          
            el_up.innerHTML = "Str = '" + str + "'";
            function check(str) {
                return /^(.+)\1+$/.test(str)
            }
              
            function gfg_Run() {
                ans = "String is not made up of same substrings";
                if (check(str)) {
                    ans = "String is made up of same substrings"; 
                }
                el_down.innerHTML = ans;
            }         
        </script
    </body
</html>                    

Output:

  • Before clicking on the button:
  • After clicking on the button:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!