Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created October 3, 2017 20:51
Show Gist options
  • Save unilecs/98b044a5ca7fa8153f5cbf0c4258451d to your computer and use it in GitHub Desktop.
Save unilecs/98b044a5ca7fa8153f5cbf0c4258451d to your computer and use it in GitHub Desktop.
Compress string: ABBCCCD -> AB2C3D
function compressString(str) {
// regexp
const zip = str.replace(/(.)\1{0,}/g,
group => (group[0] + (group.length > 1 ? group.length : '')));
return zip;
}
const str = 'AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD';
console.info("Compression of string: ", compressString(str));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment