How to check a string is entirely made up of the same substring in JavaScript ?
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 > |
chevron_right
filter_none
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 > |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Substring check in JavaScript
- JavaScript | Difference between String.slice and String.substring
- JavaScript | string.substring()
- PHP to check substring in a string
- JavaScript | Check if a string is a valid JSON string
- How to check if string contains only digits in JavaScript ?
- JavaScript | Check if a variable is a string
- How to check if a string is html or not using JavaScript?
- How to check empty/undefined/null string in JavaScript?
- JavaScript | Check if a string is a valid hex color representation
- Difference between substr() and substring() in JavaScript
- JavaScript | Insert a string at position X of another string
- How to count string occurrence in string using JavaScript?
- PHP | Program to check a string is a rotation of another string
- How to check object is an array in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.