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

  1. proceeded [a-z0-9]+: and
  2. 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:

  1. match opening extension brackets preceding character other "]":
    /[^\]]\((?=[a-z0-9]+:)/i
  2. 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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -