quick question, I have some markdown HTML content that I’d like to convert from double asterisks to bold.
the error i’m getting is: `Uncaught TypeError: Cannot call method ‘replace’ of null ‘
here’s the jsfiddle: http://jsfiddle.net/fz5ZT/9/
here’s the HTML:
<div class="comments">comment 1** is good**</div>
<div class="comments">comment 2**is bad**</div>
here’s the JS:
function markdown(markdownable){
var boldMatch = markdownable.match(/\*\*[A-Za-z0-9]+\*\*/gim),
boldReplace = boldMatch.replace(/\*\*[A-z0-9]+\*\*/gim, '<span style="font- weight:bold;color:blue;">'+boldMatch+'</span>'),
markdownable = markdownable.replace(boldMatch, boldReplace),
markdownable = markdownable.replace(/\*\*/gi, "");
return markdownable;
}
$('.comments').each(function(){
var markdownable=$(this).html(), comments=markdown(markdownable);
});
if you might be able to help i’d greatly appreciate it,
thanks,
tim
update thanks all! please see this for a working demo: http://jsfiddle.net/fz5ZT/30/
However, instead of performing a half-hearted, well-intentioned, doomed-to-fail attempt at reinventing the wheel, why not just use an existing JavaScript Markdown library?
Edit: Here’s a more robust regex that (like Markdown) requires there to be no whitespace right after the “open” or before the “close”: