Skip to content

Markdown to convert double asterisks to bold text in javascript

i’m trying to make my own markdown-able textarea like Stackoverflow has done. The goal is to allow people to type **blah blah** in a textarea and have the output in a div be <span style="font-weight:bold;">blah blah</span>.

I’m having trouble with the javascript to find and replace to the **asterisks with the HTML.

here’s a jsfiddle which has gotten the party started:

here’s the JS on that just to show you where I’m at:

$(document.body).on('click', 'button', function() {

var val=$('textarea').val();

var bolded=val.replace(/**[A-z][0-9]**/gi, '<span style="font-weight:bold;">"'+val+'" </span>');


and the HTML…

<div></div><button type="button">Markdownify</button>

any thoughts would be greatly appreciated!

thanks, tim



Your regex is broken, for one thing. You probably want something more like:


The * is a special character in regular expressions. If you want to match against a literal *, then you need to escape it with .

For instance:

However, even with this change there’s still a fair ways to go before you get to where you really want to be. For instance, your example will not work if the text area contains a mix of bold and non-bold text.