I have pieces of text where normal markdown and a custom markdown extension are mixed together. This works quite well.
[My Link Title](http://example.com)
(extension: value attribute: value)
However, I have one problem: to apply some stylings when editing the text, I need a way to match the opening bracket of extension snippet without matching the opening bracket of the markdown link.
In other words: I need a regular expression (that works in javascript) to match an opening bracket (and only the bracket) when it is
- proceeded by
[a-z0-9]+:
and - not preceded by a
]
character.
My current regular expression for that (which works well to match the extension tags opening brackets but unfortunately includes the markdown link opening brackets, too) looks like this: /\((?=[a-z0-9]+:)/i
.
I have seen people use positive lookaheads with a negation at the beginning of the regular expression like this /(?=[^\]])\((?=[a-z0-9]+:)/i
to check for this in PHP. Unfortunately, this doesn't seem to work in javascript.
Update
Thanks for your tips!
The problem I'm having is that I'm creating a "Simple Mode" syntax mode for CodeMirror to apply the highlighting. This allows you to specify a regex and a token that will be applied to the matched characters but doesn't allow any further operation on the matches. You could however write a full syntax mode where you can do this kind of operations, but I'm not capable of that :-s
After all, I went with another solution. I just created two regular expressions:
- Match all opening extension brackets with a preceding character other then "]":
/[^\]]\((?=[a-z0-9]+:)/i
- Matches all opening extension brackets without any preceding character:
/^\((?=[a-z0-9]+:)/i
Even though it isn't the cleanest possible way it seems to work quite well for now.