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

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:
javascript-string
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Convert a string to an integer in JavaScript
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?