Last active
September 9, 2022 18:11
-
-
Save sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f to your computer and use it in GitHub Desktop.
Match and replace Gist anchor with script tags
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
const regEx = /<a.*href=\"(https?\:\/\/gist\.github\.com\/\w*\/\w*)".*>.*<\/a>/gi; | |
const html = 'hola <a href="https://gist.github.com/sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f" target="_blank" rel="nofollow">https://gist.github.com/sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f</a> mundo'; | |
const finalHTML = html.replace(regEx, '<script src="$1.js"></script><noscript>Github Gist <a href="$1" target="_blank" rel="nofollow">$1</a></noscript>'); | |
document.write(finalHTML); | |
// result | |
// hola <script src="https://gist.github.com/sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f.js"></script><noscript>Github Gist <a href="https://gist.github.com/sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f" target="_blank" rel="nofollow">https://gist.github.com/sergiodxa/478eb21b6eb27d77e44bdd7d9732a46f</a></noscript> mundo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment