regex.exec() - 无法匹配字符串

时间:2022-02-23 18:59:08

Alright so here's the deal. i'm using strapdown to render markdown posts. strapdown uses marked to parse these markdown posts, which in turn uses regular expression literals.

好吧,所以这是交易。我正在使用捷联来呈现降价帖子。 stripdown使用标记来解析这些降价帖子,这些帖子又使用正则表达式文字。

I've tried to extend marked to include the following markdown "extensions" as described here (shoutout to soffes).

我试图扩展标记以包括如下所述的以下标记“扩展”(shoutout to soffes)。

Background Info

literal for underline extension: /_(.*?)_/
literal for highlight extension: /==(.*?)==/

下划线扩展的文字:/_(.*?)_/文字突出显示扩展名:/==(.*?)==/

underline test code (some code omitted, for clarity):

下划线测试代码(为清楚起见,省略了一些代码):

var src = "two new features to the marked.js inline lexer. ==highlighted text==, and _underlined text_";
var underline = /_(.*?)_/;
var cap = underline.exec(src);
document.write("<em class='underline'>"+cap[1]+"</em>");

which works as expected (outputting <em class="underline">underlined text</em>).
here's the highlight test code (again, some stuff omitted):

它按预期工作(输出带下划线的文本 )。这是突出显示测试代码(再次,省略了一些东西):

var src = "two new features to the marked.js inline lexer. ==highlighted text==, and _underlined text_";
var highlight = /==(.*?)==/;
var cap = highlight.exec(src);
document.write("<strong class='highlighted'>"+cap[1]+"</strong>");

which again, works as expected (outputting <strong class='highlighted'>highlighted text</strong>).

再次,按预期工作(输出突出显示的文字 )。

The Problem

the inline lexer for marked begins all of it's regex literals with ^. ie:

标记的内联词法分析器用^开始所有的正则表达式文字。即:

tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
link: /^!?\[(inside)\]\(href\)/,
reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/

so naturally, i feel as though i should do the same. i modify my literals like this:
literal for underline extension: /^_(.*?)_/
literal for highlight extension: /^==(.*?)==/

很自然地,我觉得我应该这样做。我修改我的文字是这样的:字面用于下划线扩展名:/^_(.*?)_/ literal for highlight extension:/^==(.*?)==/

and then, highlighting stops matching. interestingly, underline continues to match.

然后,突出显示停止匹配。有趣的是,下划线继续匹配。

I realize this might be a question that requires some knowledge of how marked works, but it usually can't hurt to ask...right?

我意识到这可能是一个需要了解标记工作方式的问题,但通常不会有问题......对吗?

Update 1

you can see what i've worked up (which requires marked.js) here. To see how marked is being told to parse markdown with my extensions, you can check out line 658 here

你可以在这里看到我的工作(这需要标记.js)。要查看如何通过我的扩展程序解析标记的标记,您可以在此处查看第658行

2 个解决方案

#1


1  

I modified the underline and stronghighlight regexes a bit, and added |== to the text regex to make it work:

我稍微修改了下划线和stronghighlight正则表达式,并在文本正则表达式中添加了| ==以使其工作:

text: /^[\s\S]+?(?=[\\<!\[_*`]|==| {2,}\n|$)/,
stronghighlight: /^==([^=]+)==/,
underline: /^_([^_]+)_/

My jsfiddle test page is here. For the sake of simplicity, I copied marked.js at the beginning and edited it in place.

我的jsfiddle测试页面在这里。为简单起见,我在开头复制了marked.js并将其编辑到位。

#2


0  

First, you are mixing = and - again: you have stronghighlight: /^--(.*?)--/, but in the text you have ==text==.

首先,你是混合=和 - 再次:你有强烈的亮点:/^--(.*?)--/,但在文本中你有== text ==。

Next, have a look at this line:

接下来,看看这一行:

text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/

This defines a normal block of text. It defines "text" as anything that is before \, <, !, [, _, or *.
Your - or = isn't there, so your formatting is consumed by text blocks.

这定义了正常的文本块。它将“text”定义为在\,<,!,[,_或*之前的任何内容。您的 - 或=不存在,因此您的格式由文本块使用。

#1


1  

I modified the underline and stronghighlight regexes a bit, and added |== to the text regex to make it work:

我稍微修改了下划线和stronghighlight正则表达式,并在文本正则表达式中添加了| ==以使其工作:

text: /^[\s\S]+?(?=[\\<!\[_*`]|==| {2,}\n|$)/,
stronghighlight: /^==([^=]+)==/,
underline: /^_([^_]+)_/

My jsfiddle test page is here. For the sake of simplicity, I copied marked.js at the beginning and edited it in place.

我的jsfiddle测试页面在这里。为简单起见,我在开头复制了marked.js并将其编辑到位。

#2


0  

First, you are mixing = and - again: you have stronghighlight: /^--(.*?)--/, but in the text you have ==text==.

首先,你是混合=和 - 再次:你有强烈的亮点:/^--(.*?)--/,但在文本中你有== text ==。

Next, have a look at this line:

接下来,看看这一行:

text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/

This defines a normal block of text. It defines "text" as anything that is before \, <, !, [, _, or *.
Your - or = isn't there, so your formatting is consumed by text blocks.

这定义了正常的文本块。它将“text”定义为在\,<,!,[,_或*之前的任何内容。您的 - 或=不存在,因此您的格式由文本块使用。