Skip to content

Instantly share code, notes, and snippets.

@alexwebgr
Last active April 1, 2016 19:12
Show Gist options
  • Save alexwebgr/f1d76c5f7c0cf3103112066b08bd9f6e to your computer and use it in GitHub Desktop.
Save alexwebgr/f1d76c5f7c0cf3103112066b08bd9f6e to your computer and use it in GitHub Desktop.
Concatenate consecutive characters followed by the number of characters if it is over 1
var input = 'aaaabbbbccccpqesrt';
var compressedString = '';
var s = input.match(/([a-zA-Z])\1*/g) || [];
s.forEach(function(value) {
var k = '';
if(value.length > 1) {
k = value.length;
}
compressedString += value.charAt(0) + k;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment