Created
January 31, 2018 12:42
-
-
Save alordiel/ed8587044be07e408f5f93b3124836b3 to your computer and use it in GitHub Desktop.
[JS] to replace a markdown for link with actual HTML link element
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
var derText = "This is some text with [a link](https://duckduckgo.com) \n\ and break line"; | |
//replace the linebreaks with <br> | |
derText = derText.replace(/(?:\r\n|\r|\n)/g, '<br>'); | |
//check for links [text](url) | |
let elements = derText.match(/\[.*?\)/g); | |
if( elements != null && elements.length > 0){ | |
for(el of elements){ | |
let txt = el.match(/\[(.*?)\]/)[1];//get only the txt | |
let url = el.match(/\((.*?)\)/)[1];//get only the link | |
derText = derText.replace(el,'<a href="'+url+'" target="_blank">'+txt+'</a>') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment