Created
March 17, 2022 15:00
-
-
Save 23maverick23/10852b333777b63be42cd453d0550fd5 to your computer and use it in GitHub Desktop.
Handy little regular expression to parse links from markdown files.
This file contains 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
// Credit: https://davidwells.io/snippets/regex-match-markdown-links | |
/* Match only links that are fully qualified with https */ | |
var regex = /^\[([\w\s\d]+)\]\((https?:\/\/[\w\d./?=#]+)\)$/; | |
var string = "[View the analytics docs](https://getanalytics.io/)"; | |
var myMatch = string.match(regex); | |
console.log(myMatch); | |
/* ["[View the analytics docs](https://getanalytics.io/)", "View the analytics docs", "https://getanalytics.io/", index: 0, input: "[View the analytics docs](https://getanalytics.io/)", groups: undefined] */ | |
// de-structure the array | |
var [ full, text, url ] = myMatch; | |
console.log(text); | |
// 'View the analytics docs' | |
console.log(url); | |
// 'https://getanalytics.io/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment