Created
May 27, 2018 18:32
-
-
Save reeddunkle/293ea2fe910fea8a71ddbb44b364da47 to your computer and use it in GitHub Desktop.
Regex examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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