javascript - Regex Not Suppose To Run In Link Tag - JS -


currently, writing regular expressions parse textarea bbcode, , replace html.

the problem @ moment regex use replace urls, replacing urls in [link] tag second time , messing them up.

i replacing [link=___]test[/link] tags so:

.replace(/(\[code\][\s\s]*?\[\/code\])|\[(link=)((http|https):\/\/[\s]{0,2000}\.[a-za-z]{2,5}(\/[^\[\]\<\>]*)?)\]([\s\s]*?)\[\/link\]/gi, function (m, g1, g2, g3, g4, g5, g6) { return g1 ? g1 : '<a href="' + g3 + '">' + g6 + '</a>'; }) 

and replacing urls in <a> tag so:

.replace(/(\[(code|link=(.*))\][\s\s]*?\[\/(code|link)\])|((http|https):\/\/[\s]{0,2000}\.[a-za-z]{2,5}(\/[^\[\]\<\>]*)?)/gi, function (m, g1, g2, g3, g4, g5) { return g1 ? g1 : '<a href="' + g5 + '">' + g5 + '</a>'; }) 

i not know how not make second regex parse urls , not change them if inside of <link> tag. ideas on how make work correctly?

i suggest adding check if there closing angle bracket , closing </a> node. if missing, not inside opening <a> tag.

here 2nd modified regex checks above conditions negative look-ahead (?![^<]*>[^<]*<\/a>):

(\[(code|link=(.*))\][\s\s]*?\[\/(code|link)\])|((http|https):\/\/[^\s<>]{0,2000}\.[a-za-z]{2,5}(\/[^\[\]\<\>]*)?)(?![^<]*>[^<]*<\/a>) 

here demo

in case have opening , closing nodes in separate string, we'd need reverse regex , use look-behind workaround "through reversal".


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -