const wordsReplace = words.replace(' ', ` ${replacement} `);
const ithem = words + ' ' + replacement ;
console.log(ithem)
if (words.length === 1) {
return phrase.replace(words,ithem);
};
if (words.length > 1) {
return phrase.replace(words,wordsReplace);
};
}
index2 - index1 + 1
и заменить его фразой replacement.function replaceSubstring(phrase, words, replacement) {
words = words.split(' ');
let regExp = new RegExp(`\\b${words[0]}\\b(.*?)\\b${words[1]}\\b`);
return phrase.replace(regExp, `${words[0]} ${replacement} ${words[1]}`);
}
replaceSubstring('She is a robot', 'She robot', 'dreamed of a'); // 'She dreamed of a robot'