var str = 'AAAADEEESSQQQQQQ'; var result = str.replace(/(.)\1+/g, function(m, c) { return c + m.length; }); alert(result);