-1

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>
Ludwig
  • 1,401
  • 13
  • 62
  • 125
  • 1
    What is the context? Is it in node (server) code? – Mosh Feu Sep 28 '17 at 16:47
  • Please show us an example of the text you expect to parse – Steven Wexler Sep 28 '17 at 16:49
  • Why does this look similar to: https://stackoverflow.com/questions/46469541/js-regex-for-finding-urls-in-body-text-not-working/46469723#46469723 – Rajesh Sep 28 '17 at 16:50
  • node is the object, that has text – Ludwig Sep 28 '17 at 16:53
  • @Marco As said in my answer in associated link, when dealing with HTML string, using regex and string manipulation will only make things worse. Using an in-memory element will help and ease manipulation – Rajesh Sep 28 '17 at 16:56
  • So you don't want http://link.html -> http:// target="_blank"/link.html? – Steven Wexler Sep 28 '17 at 17:04
  • Please include output showing what you want. – Steven Wexler Sep 28 '17 at 17:27
  • Your question makes reference to `href='www.link.com'` but consider that this link will redirect within your website (it will redirect to something like `http://www.yourdomain.com/www.link.com`), so maybe that isn't the best example to use. – JonahAaron Sep 28 '17 at 20:37

1 Answers1

1

Try this out:

document.querySelectorAll('a[href^="http"]').forEach(function(e) {
  e.setAttribute('target', '_blank');
});
JonahAaron
  • 398
  • 1
  • 9
  • Please refer to link under comments. This solution will not help – Rajesh Sep 28 '17 at 16:53
  • I see your edit. This was an answer to your original question but I will see if I can edit to fit your new parameters. – JonahAaron Sep 28 '17 at 16:55
  • @Rajesh in your edit you reference a link that points to "www.link.com". Note that this is not an offsite link as it will send users to http://yoursite.com/www.link.com – JonahAaron Sep 28 '17 at 16:57
  • I have no idea what you mean. What edit are you talking about? – Rajesh Sep 28 '17 at 17:08
  • @Rajesh no worries, I was referring to your most recent edit here: https://stackoverflow.com/posts/46474353/revisions – JonahAaron Sep 28 '17 at 17:10
  • @Rajesh My mistake! I incorrectly thought you were OP. I still believe that matching a relative anchor is probably not what the asker wants but I will let them clarify that if it is the case. – JonahAaron Sep 28 '17 at 17:16