This is a follow up to: Javascript regex placeholder prints string instead of its value
I am trying to build a javascript function that looks for a pattern and converts it into a link.
var re = /Ticket-([0-9]*?(?=-)-[0-9]*)/;
var str = 'ASD Ticket-492-367 - Make my day.';
t = str.replace(re,'<a href="http://myworld/ticket/$1">$&</a>')
I now have run into a problem where if my string already contains <a>
tags it tries to latch on extra tags to it which makes the output funky. Is there a specific jQuery/JS way for the regex matching to somehow skip contents that's placed inside a particular tag. Like, somehow wrap a <div>
tag around the contents, then parse it as a DOM node...and operate on it that way. I am very new to JS so apologize if my thinking is completely off the mark
Update: for the use case. Lets say the text I am getting already has a link such as the one below:
<a href="http://myworld/ticket/4385-21557">Ticket-4385-21557 - abc xyz</a>
This will wrap another tag around matched string 'Ticket-4385-21557'. This is legacy string thats already in the system and cannot change it. So the idea is to work around it by skipping the string inside of the <a>
tag