CoryG CoryG - 1 year ago 150
Javascript Question

Non-Greedy Backwards JavaScript Regex?

I have the following JavaScript regex:

/\!\[(.*?)\]\((.+?) \"(.*?)\"\)/g

Which is applied to a string like:

![test](<br />![test]( "test")<br />![test](<br />![test]( "test")

regex101 link

The issue I'm having is that there should be 4 distinct markdown tags in the search string, two of which should match, however the two matches coming back include markdown tags.

This seems to boil down to the regex parser looping through, grabbing the first possible match starting point and continuing on searching for the remainder of the match without regard to the fact there is a later possible starting point within the match. I thought I could get around this with the non-greedy
modifier, however that doesn't seem to be the case.

Any suggestions?

Answer Source

Two of the Markdown links don't have descriptions, but the description isn't optional in your regular expression.

/\!\[(.*?)\]\((.+?)(?: \"(.*?)\")?\)/g

It might be better to exclude certain characters in the link address part to avoid unintended long matches like this (although you should use an existing Markdown parser if you need robustness).

/\!\[(.*?)\]\(([^)]+?)(?: \"(.*?)\")?\)/g
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download