Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match markdown link

I have a string with markdown in it. I am trying to strip all markdown using regex but am having trouble with matching links. Here's how far I got:

function stripMarkdown(text) {
  var str = String(text).replace(/(__|\*|\#)/gm, '');
  return str;
}

var testStr = '# This is the title. ## This is the subtitle. **some text** __some more text__. [link here](http://google.com)'

stripMarkdown(testStr);

So I believe the above will strip all unwanted markdown except the link. How do I handle that? Also, if there's a better way to do this, please let me know.

Desired outcome:

This is the title. This is the subtitle. some text some more text. link here
like image 843
CaribouCode Avatar asked May 26 '16 13:05

CaribouCode


1 Answers

I came up with this regex:

(?:__|[*#])|\[(.*?)\]\(.*?\)

var str = '# This is the title. ## This is the subtitle. **some text** __some more text__. [link here](http://google.com)'

document.write(String(str).replace(/(?:__|[*#])|\[(.*?)\]\(.*?\)/gm, '$1'));
like image 53
Thomas Ayoub Avatar answered Oct 07 '22 11:10

Thomas Ayoub