1

I am trying to replace urls between the href tag but unfortunately i don't know the javascript equivalent of

(?<=href=(\"|'))[^\"']+(?=(\"|'))

Javascript is missing the positive look behind.

Any idea how i can achieve this please ? Thank you !

DanteDiaze
  • 128
  • 1
  • 6
  • 1
    Just have to post when people use reg exp on html https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – epascarello Oct 27 '17 at 14:49
  • If you show a larger context for what you're trying to do, we can perhaps offer better ways to solve this problem than using a regex (such as using the DOM API or an actual HTML parser). This question is kind of like "I'm down this narrow path of a solution to a larger problem and ran into this dead-end. How do I solve this dead-end?" when, in reality, there are probably other ways to solve the problem that don't go down this dead-end. But, without sharing the overall problem you're trying to solve, you've make it so we can't offer our best help. – jfriend00 Oct 27 '17 at 15:04

2 Answers2

1

It is just an attribute on the anchor element:

function magic(){
    var a=document.getElementById("anchor");
    a.href="http://www.stackoverflow.com";
}
<a href="#" id="anchor">Trallala</a>
<button onclick="magic()">Pushme</button>

EDIT: Apparently links are not very welcome in these code snippets, but you can check the change via hovering, and you can also open the link in a new window/tab

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

In general, trying to modify HTML with regular expressions is really difficult, dangerous, and hard to maintain. You'll have a much better time using a HTML manipulation library like jQuery:

<a id='myLink' href='http://fish.com'></a>

var $link= $("'#myLink");
$link.attr("href", "http://melons.com');
Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20