Regular match against opening parentheses not preceded by "]"
I have snippets of text where a regular markdown and a custom markup extension are mixed together. This works well enough.
[My Link Title](http://example.com)
(extension: value attribute: value)
However, I have one problem, apply some styles when editing text, I need a way to match the open parenthesis of the extension chunk without matching the open parenthesis of the markdown link.
In other words: I need a regex (which works in javascript) to match the opening parenthesis (and only the parenthesis) when it is
- performed
[a-z0-9]+:
and - is not preceded by a character
]
.
My current regular expression for this (which works well to compare the parentheses extensions, but, unfortunately, includes brackets opening links by default) as follows: /\((?=[a-z0-9]+:)/i
.
I've seen people use regex negative negative imagery, sort of /(?=[^\]])\((?=[a-z0-9]+:)/i
to test this in PHP . Unfortunately this doesn't work in javascript.
Update
Thanks for your advice!
The problem I am facing is that I am creating a "Simple Mode" syntax for CodeMirror in order to apply highlighting. This allows you to specify a regular expression and token to be applied to the matched characters, but does not allow further match operations. However, you could write a full syntax mode where you can do operations like this, but I am not capable of that: -s
In the end, I went with a different solution. I just created two regular expressions:
- Match all the opening parentheses of the extension to the previous character followed by "]":
/[^\]]\((?=[a-z0-9]+:)/i
- Matches all opening parentheses of an extension without a leading character:
/^\((?=[a-z0-9]+:)/i
Even though this is not the cleanest way, it seems to work very well now.
source to share
Using the skip trick :
\[[^\]]+\]\([^\)]+\)|(\(\b)
-
\[[^\]]+\]\([^\)]+\)
- match[]()
links (you can also write\[.*?\]\(.*?\)
if it's too confusing), OR - -
(\(\b)
- match and freeze open parentheses that appear immediately before an alphanumeric character.
Working example: https://regex101.com/r/tY9sS4/1
You will need to see the result and only handle the matches where the group is $1
grouped and ignore the rest of the matches.
source to share