Is it possible to replace an <a> tag to it's href link within a string without using innerHTML?

I have a JavaScript string that looks like this

const link = `
  Checkout My Cheat Sheet:

  <a target="_blank" href="https://tailwindcomponents.com/cheatsheet/"/>

  It's awesome
`;

I want to convert the above text to this

const link = `
  Checkout My Cheat Sheet:

  https://tailwindcomponents.com/cheatsheet/

  It's awesome
`;

Although I can use innerHTML, my text might contain other script tags, which might cause XSS.

Replacing <a> tag is enough for me right now.

Is there any possible string solution?

85 thoughts on “Is it possible to replace an <a> tag to it's href link within a string without using innerHTML?”

Leave a Comment