var str = "a mo mo mo fw mo mo"
var str2 = "";
do{
str2 = str;
str = str.replace(/\s(\w+\s)\1/, " $1");
}while(str.length != str2.length)
str = str.replace(/^(\w+\s)\1/, "$1").replace(/(\s\w+)\1$/, "$1");
console.log(str);
var str = "a mo mo mo fw mo mo"
var str2 = "";
do{
str2 = str;
str = str.replace(/\s(\w+\s)\1/, " $1");
}while(str.length != str2.length)
str = str.replace(/^(\w+\s)\1/, "$1").replace(/(\s\w+)\1$/, "$1");
console.log(str);