var str = ":test:test:test:test:test:", reg = /(:\w+)(?=\1)+/g; str = str.replace(reg, "$1: "); alert(str);