Skip to content

Instantly share code, notes, and snippets.

@h2oota
Created April 9, 2020 00:59
Show Gist options
  • Save h2oota/ab59ccee2ed35232774d802651ae7563 to your computer and use it in GitHub Desktop.
Save h2oota/ab59ccee2ed35232774d802651ae7563 to your computer and use it in GitHub Desktop.
const s = '#6c757d';
console.log(s.replace(/#([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])/i,
(m, r, g, b) => `rbd(${parseInt(r, 16)}, ${parseInt(g, 16)}, ${parseInt(b, 16)})`));
console.log(s.replace(/#([0-9a-f]{6})/i, (m, rgb) => (h => `rbg(${h >> 16}, ${(h >> 8) & 255}, ${h & 255})`)(parseInt(rgb, 16))));
console.log(s.replace(/#[0-9a-f]{6}/i, xrgb => (h => `rbg(${h >> 16}, ${(h >> 8) & 255}, ${h & 255})`)(parseInt(xrgb.substring(1), 16))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment