如果没有反斜杠,如何匹配粗体的markdown? [英] How to match bold markdown if it isn't preceded with a backslash?

查看:109
本文介绍了如果没有反斜杠,如何匹配粗体的markdown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要匹配粗体的markdown.以下是一些示例:

I'm looking to match bolded markdown. Here are some examples:

qwer *asdf* zxcv匹配*asdf*

qwer*asdf*zxcv匹配*asdf*

qwer \*asdf* zxcv不匹配

*qwer* asdf zxcv匹配*qwer*

像这样(?<!\\)\*(.*)\*的作品背后的负面表情.

A negative look behind like this (?<!\\)\*(.*)\* works.

除非Firefox中不支持浏览器,所以我不能使用它.

Except there is no browser support in Firefox, so I cannot use it.

类似地,我可以很接近(^|[^\\])\*(.*)\*

Similarly, I can get very close with (^|[^\\])\*(.*)\*

问题是有两个捕获组,我需要第二个捕获组的索引,而Javascript只返回第一个捕获组的索引.在这种情况下,我可以通过仅添加1来对其进行创可贴,但是在其他情况下,此技巧将无法正常工作.

The issue is that there are two capture groups, and I need the index of the second capture group, and Javascript only returns the index of the first capture group. I can bandaid it in this case by just adding 1, but in other cases this hack will not work.

之所以这样做,是因为我试图用React组件替换Markdown的一小部分.例如,我正在尝试转换此字符串:

My reasoning for doing this is that I'm trying to replace a small subset of Markdown with React components. As an example, I'm trying to convert this string:

qwer *asdf* zxcv *123*

进入此数组:

[ "qwer ", <strong>asdf</strong>, " zxcv ", <strong>123</strong> ]

其中第二个和第四个元素是通过JSX创建的,并作为数组元素包含在其中.

Where the second and fourth elements are created via JSX and included as array elements.

推荐答案

您还需要考虑到,当反斜杠出现在星号之前时,它本身可能会被反斜杠转义,在这种情况下星号被认为是粗体标记的开始.除非该命令前面也有反斜杠,否则...等.

You will also need to take into account that when a backslash occurs before an asterisk, it may be one that is itself escaped by a backslash, and in that case the asterisk should be considered the start of bold markup. Except if that one is also preceded by a backslash,...etc.

所以我建议使用以下正则表达式:

So I would suggest this regular expression:

((?:^|[^\\])(?:\\.)*)\*((\\.|[^*])*)\*

如果目的是将其替换为标签,例如<strong> ... </strong>,则只需使用JavaScript的replace如下:

If the purpose is to replace these with tags, like <strong> ... </strong>, then just use JavaScript's replace as follows:

let s = String.raw`now *this is bold*, and \\*this too\\*, but \\\*this\* not`;
console.log(s);

let regex = /((?:^|[^\\])(?:\\.)*)\*((\\.|[^*])*)\*/g;
let res = s.replace(regex, "$1<strong>$2</strong>");
console.log(res);

如果将加粗的单词转换为React组件并与其他纯文本一起存储在数组中,则可以使用splitmap:

If the bolded words should be converted to a React component and stored in an array with the other pieces of plain text, then you could use split and map:

let s = String.raw`now *this is bold*, and \\*this too\\*, but \\\*this\* not`;
console.log(s);

let regex = /((?:^|[^\\])(?:\\.)*)\*((?:\\.|[^*])*)\*/g;
let res = s.split(regex).map((s, i) =>
    i%3 === 2 ? React.createComponent("strong", {}, s) : s
);

由于分隔符"中有两个捕获组用于split调用,一个捕获组具有前面的字符,第二个捕获组是单词本身,因此split结果中的每个第三项都是要捕获的单词粗体,因此i%3表达式.

Since there are two capture groups in the "delimiter" for the split call, one having the preceding character(s) and the second the word itself, every third item in the split result is a word to be bolded, hence the i%3 expression.

这篇关于如果没有反斜杠,如何匹配粗体的markdown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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