Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex.exec() - failing to match string

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.

I've tried to extend marked to include the following markdown "extensions" as described here (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: /^==(.*?)==/

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

like image 451
bengreenier Avatar asked Nov 11 '22 23:11

bengreenier


1 Answers

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

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.

like image 130
kol Avatar answered Nov 14 '22 22:11

kol