Question

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: http://jsfiddle.net/trpeters1/2LAL4/14/

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

$('div').html(bolded);
});

and the HTML...

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

any thoughts would be greatly appreciated!

thanks, tim

Était-ce utile?

La solution

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

/\*\*[A-z0-9]+\*\*/gi

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: http://jsfiddle.net/2LAL4/22/

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.

Autres conseils

The other answers fail when a char is immediately before or after the asterisks.

This works like markdown should:

function bold(text){
    var bold = /\*\*(.*?)\*\*/gm;
    var html = text.replace(bold, '<strong>$1</strong>');            
    return html;
}
    
var result = bold('normal**bold**normal **b** n.');
document.getElementById("output").innerHTML = result;
div { color: #aaa; }
strong { color: #000; }
<div id="output"></div>

None of the provided answers works in all cases. For example, the other solutions wont work if we have a space next to the double star, ie:

This will ** not ** be bold

So I wrote this:

function markuptext(text,identifier,htmltag)
{
    var array = text.split(identifier);
    var previous = "";
    var previous_i;
    for (i = 0; i < array.length; i++) {
        if (i % 2)
        {
            //odd number
        }
        else if (i!=0)
        {
            previous_i = eval(i-1);
            array[previous_i] = "<"+htmltag+">"+previous+"</"+htmltag+">";
        }
        previous = array[i];
    }
    var newtext = "";
    for (i = 0; i < array.length; i++) {
        newtext += array[i];
    }
    return newtext;
}

Just call it like this:

thetext = markuptext(thetext,"**","strong");

and it will work in all cases. Of course, you can also use it with other identifiers/html-tags as you like (the stackoverflow preview should have this too).

Why create from scratch? With so many open source editors out there, you should pick a code base you like & go from there. http://oscargodson.github.com/EpicEditor/ http://markitup.jaysalvat.com/home/

custom component in react who receives bold like boolean

{(() => {
  const splitText = theText.split('**');
  return (
    <TextByScale>
      {splitText.map((text, i) => (
        <TextByScale bold={!!(i % 2)}>{text}</TextByScale>
      ))}
    </TextByScale>
  );
})()}

If you are using jQuery, replace this:

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

with this:

$("button").click(function () {

The following regular expression will find your asterisk-wrapped text:

/\x2a\x2a[A-z0-9]+\x2a\x2a/

I updated your fiddle as an example: http://jsfiddle.net/2LAL4/30/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top