Reputation: 66550
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?:\/\/(?:(?:(?!&[^;]+;)|(?=&))[^\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
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?:\/\/(?:(?:(?!&[^;]+;)|(?=&))[^\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