I am trying to find in a text all links that have http or https and add target blank to them it they exist with this code:
const text = this.node.body;
const regex = /https?:\/\//i;
let newStr = text.replace(regex, '$& target="_blank"');
return newStr;
But, it doesn't work, the links that have http or https are not getting target blank. What is the correct way of doing this? This is the example text:
<p><a href='www.link.com'>Link</a></p><p><div><img src='http://image.jpg' /></div></p><p><a href='http://link.html'>link</a></p>
And this is the result of the code:
<p><a href='www.link.com'>Link</a></p><p><div><img src='http:// target="_blank"/image.jpg' /></div></p><p><a href='http://link.html'>link</a></p>