[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Check if a string consists of a repeated character sequence

Check if a string consists of a repeated character sequence

JavaScript

const consistsRepeatedSubstring = (str) => `${str}${str}`.indexOf(str, 1) !== str.length

TypeScript

const consistsRepeatedSubstring = (str: string): boolean => `${str}${str}`.indexOf(str, 1) !== str.length

Examples

consistsRepeatedSubstring('aa') // true
consistsRepeatedSubstring('aaa') // true
consistsRepeatedSubstring('ababab') // true
consistsRepeatedSubstring('abc') // false