I have a regular expression which will parse the content of a message and convert plain text hyperlinks into HTML hyperlinks.
message = message.replaceAll("(?:https?|http?)://[\\w/%.\\-?&=!#]+",
"<a href='$0' target='_blank'>$0</a>");
It is fine when converting http or https hyperlinks.
The problem is that I also have other hyperlinks enclosed within bbcode tags which I do not want the regular expression to converted into HTML hyperlinks.
E.g. [IMG]http://www.google.com/img.png[/IMG] or [YOUTUBE]http://www.youtube.com[/YOUTUBE]
How do I change my regular expression to prevent the regex from converting bbcode hyperlinks?
You can use negative lookbehind.
The
(?<!\])will only allow the rest of the URL pattern to match if it does not immediately follow a']'.