Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article
Like Article

In this article, we are 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 the test() method to test a pattern in a string.
  • The test() method returns true if the match is found, otherwise, it returns false.

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

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        How to check a string is entirely
        made of same substring ?
    </h3>
  
    <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>

Output:

Check a string is entirely made up of the same substring

Check a string is entirely made up of the same substring

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

html




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        How to check a string is entirely
        made of same substring ?
    </h3>
  
    <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>

Output:

Check a string is entirely made up of the same substring

Check a string is entirely made up of the same substring


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!