Find all URLs in a string except the URL inside a and img tag and make a link using JavaScript

Today we will show you how to find all URLs in a string except the URL inside a and img tag and make a link using JavaScript.

Previously, we wrote a similar article but there we did not have the ability to ignore the URLs in the anchor tag and image tag.

Here we will take the following string which we will test with the previous code and then try with the updated new code.

String: Visit www.cluemediator.com and <a href="https://www.cluemediator.com/subscribe" target="_blank" rel="noopener noreferrer">subscribe us</a> for regular updates.

Replace a hyperlink instead of the text URLs

This is the code of the previous article where we are changing the hyperlink instead of the text URL without checking the tag.

Reference: Find URLs in string and make a link using JavaScript

You can see that the given code is also changing the link to the anchor tag. Similarly, it will also change the image tag URL. Let’s solve this problem.

Replace a hyperlink instead of the text URLs except a tag and img tag

Now we will slightly update the Regular Expression to ignore a tag and img tag from the string. Use the following code to check the same string.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *