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 !

2
  • 1
    Just have to post when people use reg exp on html stackoverflow.com/questions/1732348/… Commented Oct 27, 2017 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. Commented Oct 27, 2017 at 15:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.