Forked from DavidWells/regex-find-image-links-in-markdow.js
Created
January 2, 2022 00:15
-
-
Save Uvacoder/a4065edf7b572a5e9ec6a1608ab31f28 to your computer and use it in GitHub Desktop.
Find images in markdown file Regex
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
| var regexMdLinks = /(?:['"(])((?:\/|https?:\/\/)[\w\d./?=#%+&]+)/gmi | |
| // Example md file contents | |
| var mdContents = ` | |
| Lorem ipsum dolor sit amet, consectetur adipiscing elit.. | |
| [hello link](/admin/table_edit/table_edit.cfm?action=edit&table_name=organizationsXcategories) | |
| Lorem ipsum dolor sit amet, consectetur adipiscing elit.. | |
| [otherLink](https://xyz.com) | |
| <a href='/fun.jpg'>ncie></a> | |
| <a href='fun.jpg'>ncie></a> | |
| <a href='https://google.com'>ncie></a> | |
| https://google.com | |
| [third link](https://google.com) | |
| Lorem ipsum dolor sit amet, consectetur adipiscing elit.. | |
| [third link](https://google.com) | |
| <a href="https://davidwells.io/snippets/regex-match-markdown-links.svg">test</a> | |
| [third link](https://google.com/image.jpeg) | |
| \`\`\` | |
| const url = 'https://lol.com' | |
| \`\`\` | |
| ` | |
| var matches = mdContents.match(regexMdLinks) | |
| if (matches) { | |
| var foundLinks = matches.map((m) => m.replace(/^['"(]/, '')) | |
| console.log('foundLinks', foundLinks) | |
| var imageLinks = foundLinks.filter((link) => { | |
| return link.match(/(png|jpe?g|gif|webp|svg)$/) | |
| }) | |
| console.log('foundLinks', foundLinks) | |
| console.log('imageLinks', imageLinks) | |
| } | |
| console.log('links', matches) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment