jcubic
jcubic

Reputation: 66550

How to match URLs but not in brackets

I have urls that maybe inside brackets like this:

[[!;;;;http://example.com/foo]some text]

how to match URL but not when they are in brackets like that, some text may also be the URL. I need to replace all URLs to be in that format.

So far I have regex that match URLs:

var url_re = /(\bhttps?:\/\/(?:(?:(?!&[^;]+;)|(?=&amp;))[^\s"'<>\]\[)])+\b)/gi;

Example input:

http://example.com/foo [[!;;;;http://example.com/foo]some text] http://example.com/foo

output:

[[!;;;;http://example.com/foo]http://example.com/foo] [[!;;;;http://example.com/foo]some text] [[!;;;;http://example.com/foo]http://example.com/foo]

Upvotes: 1

Views: 99

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626929

You may add a (?![^[\]]*]) negative lookahead that will avoid matching the pattern before a closing ] that is not preceded with any other [ or ]:

\b(https?:\/\/(?:(?:(?!&[^;]+;)|(?=&amp;))[^\s"'<>\][)])+)\b(?![^[\]]*])

and replace with [[!;;;;$1]$1]. See the regex demo

Another option is to match and capture all inside [...[...]...] and then use a callback inside String.replace() to handle each capture properly, but the above seems "cleaner" and more direct.

Upvotes: 1

Related Questions