Skip to content

Instantly share code, notes, and snippets.

@reeddunkle
Created May 27, 2018 18:32
Show Gist options
  • Select an option

  • Save reeddunkle/293ea2fe910fea8a71ddbb44b364da47 to your computer and use it in GitHub Desktop.

Select an option

Save reeddunkle/293ea2fe910fea8a71ddbb44b364da47 to your computer and use it in GitHub Desktop.
Regex examples
function withExec() {
const scopesExpression = /\/scopes\/([0-9]*)/g;
const { 1: scopeId1 } = scopesExpression.exec("/scopes/1234");
const { 1: scopeId2 } = scopesExpression.exec("/scopes/5678");
return [scopeId1, scopeId2];
// Uncaught TypeError: Cannot destructure 'undefined' or 'null'.
}
function withMatch() {
const scopesExpression = /\/scopes\/([0-9]*)/;
const { 1: scopeId1 } = "/scopes/1234".match(scopesExpression);
const { 1: scopeId2 } = "/scopes/5678".match(scopesExpression);
return [scopeId1, scopeId2];
// ["1234", "5678"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment