Markdown将双星号转换为javascript中的粗体文本 [英] Markdown to convert double asterisks to bold text in javascript

查看:166
本文介绍了Markdown将双星号转换为javascript中的粗体文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像Stackoverflow那样制作我自己的可降价的textarea。目标是允许人们在textarea中键入 ** blah blah ** 并将div中的输出设为< span style = font-weight:bold;> blah blah< / span>

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>.

我在使用javascript查找和替换时遇到问题使用HTML的**星号。

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

这里是一个让派对开始的jsfiddle: http://jsfiddle.net/trpeters1/2LAL4/14/

here's a jsfiddle which has gotten the party started: http://jsfiddle.net/trpeters1/2LAL4/14/

这里的JS只是为了告诉你我在哪里:

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

和HTML ...

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

任何想法都将不胜感激!

any thoughts would be greatly appreciated!

谢谢,

推荐答案

一方面,你的正则表达式被打破了。你可能想要更像这样的东西:

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 \.

例如: 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.

这篇关于Markdown将双星号转换为javascript中的粗体文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆