如何在没有给定模式的情况下将字符串拆分为相同的部分?

I have a function that returns every time a different string but with a specific random pattern, this pattern could be as simple as "a, b, c, a, b, c" or something much more complicated.

So what I need is to write a function that searches in the string for a pattern and return it.

There is only one condition to be considered, let's say we have this string for example: "a, b, a, b, c, a, b, a, b, c"

In that string you can't say that "a, b" is a pattern, to consider a specific string as a pattern then it should be longer than the remaining string, in that case, "a,b" is (2 digits * 2) = 4 and the remaining is "c, a, b, a, b, c" 6 digits, so what consider a real pattern is "a, b, a, b, c".

I was about writing a function to do that but I know it's going to be a complicated one so I thought to ask SO before if maybe there is a built-in functionality in PHP or Javascript which can do something close from that or makes the job easier for me, so any ideas guys?

You can do this with a regexp: ^([a-z]{2,})(?:\1)+$. The regex uses a recursive pattern to match any string that has a repeating pattern, placing the pattern in the first group. This is how to use it in PHP:

Edit

The regex has been updated to allow an incomplete pattern on the end (as long as there has been one repeat) and then that is checked that it matches for its length against the pattern, to allow matching strings such as abcabca.

function check_match($string) {
    if (preg_match('/^([a-z]{2,})(?:\1)+(.*)$/', $string, $matches)) {
        return strlen($matches[2]) == 0 || $matches[2] == substr($matches[1], 0, strlen($matches[2])) ? $matches[1] : false;
    }
    return false;

}

foreach (array("abca", "abcabcab", "abcabcabcabc", "ababcababc", "aabbaabb", "aabaab", "aabaabd", "abcd", "aabcaabc", "abcabca") as $str) {
    if ($pattern = check_match($str))
        echo "$str matched $pattern
";
    else 
        echo "no pattern for $str
";
}

Output:

no pattern for abca
abcabcab matched abc
abcabcabcabc matched abcabc
ababcababc matched ababc
aabbaabb matched aabb
aabaab matched aab
no pattern for aabaabd
no pattern for abcd
aabcaabc matched aabc
abcabca matched abc

Sounds like the simplest solution would be to start from the beginning of the string and find the smallest substring starting from index 0 such that repeating that substring some number of times results in the input string:

const findPattern = str => {
  const { length } = str;
  for (let i = 1; i < str.length - 1; i++) {
    const testPattern = str.slice(0, i);
    const repeats = Math.ceil(length / i);
    if (str === testPattern.repeat(repeats).slice(0, length)) return testPattern;
  }
  return 'No pattern found';
}
console.log(findPattern('ababcababc'));
console.log(findPattern('aabbaabb'));
console.log(findPattern('aabaab'));
console.log(findPattern('aabaabaab'));
console.log(findPattern('abcdabc'));
console.log(findPattern('abcd'));

</div>