const str = 'яблоко, груша (1, 2)'
яблоко, груша (1; 2)
function replace(str) {
let nesting = 0;
let result = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === '(') {
nesting++;
} else if (str[i] === ')') {
nesting--;
} else if (str[i] === ',' && nesting !== 0) {
result += ';';
continue;
}
result += str[i];
}
return result;
}
console.log(replace(str));
console.log(str.replace(/\((.*?),(.*?)\)/, '($1;$2)'));