js版
var gcdOfStrings = function (str1, str2) {
let small, big;
if (str1.length > str2.length) {
small = str2;
big = str1;
} else {
small = str1;
big = str2;
}
let common = [];
for (let i = 1; i <= small.length; i++) {
let sub = small.substr(0, i);
if (small.replace(new RegExp(sub, 'g'), '').length === 0) {
common.push(sub)
}
}
let res = "";
common.forEach(e => {
if(big.replace(new RegExp(e, 'g'), '').length === 0){
if (res) {
if(e.length > res.length){
res = e;
}
} else {
res = e;
}
}
});
return res;
}