Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  68] [ 3]  / answers: 1 / hits: 16097  / 8 Years ago, thu, may 26, 2016, 12:00:00

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

More From » regex

 Answers
82

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'));




[#62010] Wednesday, May 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reina

Total Points: 241
Total Questions: 82
Total Answers: 94

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;