Skip to content

Instantly share code, notes, and snippets.

@ozrabal
Created July 5, 2018 06:56
Show Gist options
  • Select an option

  • Save ozrabal/4d4e0a94cf2fb27a74149c029c095fe9 to your computer and use it in GitHub Desktop.

Select an option

Save ozrabal/4d4e0a94cf2fb27a74149c029c095fe9 to your computer and use it in GitHub Desktop.
regex for slack style emoji shortcode
const regex = /:[^:\s]*(?:::[^:\s]*)*:/g;
const str = `:any-non-whitespace:
:kudos:
:text1:sample2:
:@(1@#\$@SD: :s:
:nospace::inbetween: because there are 2 colons in the middle
:nospace:middle:nospace:`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment