Search code examples
javascriptregexmarkdownregex-greedy

RegEx for matching square bracketed words with no parenthesis following


I'd like to extend the syntax of Markdown with a simple pre-processing option.

When the text contains some words in squared brackets, but no parenthesis follows it:

Example text with [some reference] to show.

I'd like to extend it so that it becomes a self-referencing link:

Example text with [some reference](some-reference.html) to show.

Creating the result itself is something I can do on my own, but I have no idea, how can I match exactly only these expressions:

A word [and more] in the text.                    // this matches
A word [and more](stackoverflow.com) in the text. // this doesn't

Solution

  • Something like this

    \[([^\[\]]*)\](?!\([^()]*\))

    https://regex101.com/r/64unOk/1