javascript - Regex to match opening parentheses not preceded by "]" -
i have pieces of text normal markdown , custom markdown extension mixed together. works quite well.
[my link title](http://example.com) (extension: value attribute: value)
however, have 1 problem: apply stylings when editing text, need way match opening bracket of extension snippet without matching opening bracket of markdown link.
in other words: need regular expression (that works in javascript) match opening bracket (and bracket) when is
- proceeded
[a-z0-9]+:
and - not preceded
]
character.
my current regular expression (which works match extension tags opening brackets unfortunately includes markdown link opening brackets, too) looks this: /\((?=[a-z0-9]+:)/i
.
i have seen people use positive lookaheads negation @ beginning of regular expression /(?=[^\]])\((?=[a-z0-9]+:)/i
check in php. unfortunately, doesn't seem work in javascript.
update
thanks tips!
the problem i'm having i'm creating "simple mode" syntax mode codemirror apply highlighting. allows specify regex , token applied matched characters doesn't allow further operation on matches. write full syntax mode can kind of operations, i'm not capable of :-s
after all, went solution. created 2 regular expressions:
- match opening extension brackets preceding character other "]":
/[^\]]\((?=[a-z0-9]+:)/i
- matches opening extension brackets without preceding character:
/^\((?=[a-z0-9]+:)/i
even though isn't cleanest possible way seems work quite now.
using skip , match trick:
\[[^\]]+\]\([^\)]+\)|(\(\b)
\[[^\]]+\]\([^\)]+\)
- match[]()
links (you can write\[.*?\]\(.*?\)
if confusing), or -(\(\b)
- match and capture open parentheses directly before alphanumeric character.
working example: https://regex101.com/r/ty9ss4/1
you have see result , process matches $1
grouped captured, , ignore other matches.
Comments
Post a Comment