字符串 HG[3|A[2|BC]]F
為壓縮形式
展開規(guī)則為 [2|BC] => BCBC
字符串 HG[3|A[2|BC]]F
的展開結(jié)果為 HGABCBCABCBCABCBCF
let str = 'HG[3|A[2|BC]]F';
const flatString = function (str) {
let reg = /\[(\d)\|(\w+)\]/g;
while (str.match(reg)) {
str = str.replace(reg, function($, $1, $2) {
return $2.repeat($1);
})
}
return str;
}
flatString(str); // HGABCBCABCBCABCBCF