javascript - REGEX: Add a pattern to not match my pattern in certain scenarios -


i have wonderful pattern seems work me match links/urls

/(\b(https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|])/gi 

i'm using pattern find links , insert bbcode around links, if example text http://www.google.com, output [url]http://www.google.com[/url]

the hard part if input text has bbcode in it, example [url]http://www.google.com[/url] output gets 2x bbcode placed around it. [url][url]http://www.google.com[/url][/url]

im hoping somehow not match links have [url] encasing link.

summary

current pattern: /(\b(https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|])/gi

current output:

http://www.google.com ==> [url]http://www.google.com[/url]    [url]http://www.google.com[/url] ==> [url][url]http://www.google.com[/url][/url] [url="http://www.google.com"]http://www.google.com[/url] ==> [url][url="http://www.google.com"]http://www.google.com[/url][/url] 

desired output:

http://www.google.com ==> [url]http://www.google.com[/url]    no matching [url]http://www.google.com[/url] ==> [url]http://www.google.com[/url] no matching [url="http://www.google.com"]http://www.google.com[/url] ==> [url="http://www.google.com"]http://www.google.com[/url] 

you can use discard technique adding discard patterns @ beginning of regex this. can add discard pattern rid of tags this:

\[.+?\].*?\[\/.+?\]|(\b(?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]) discard --^ [...]...[/...] 

regular expression visualization

working demo

as can see in screenshot below, have matches in red/green (your original regex) , in blue discarded patterns.

enter image description here

just let know, discard pattern consists of adding patterns want rid off separated or @ right side of regex , use capturing group @ rightest side this:

discard patt1 | discard patt2 | discard pattn | (capture content) 

regular expression visualization


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -