0

I have a string extracted from a div and stored in variable "str". I now need to extract the <a href="blah">...</a> subset of it.

str = '<div id="xyz"><p>This is a paragraph</p><a href="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=brd&FlightID=2997227&Page=&PluID=0&Pos=9088" target="_blank"><img src="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=bsr&FlightID=2997227&Page=&PluID=0&Pos=9088" border=0 width=300 height=250></a></div>';

Thanks in advance for any help with this.

1
  • One thing that may help with the answers: do you really want to operate on the string itself? Or do you want to extract the information from the DOM? If the former, are there any rules for how the string is formed? That is, is href the first or only attribute? How many a tags are there in the string? Commented Jul 28, 2011 at 6:46

5 Answers 5

2

You could try something like the below:

var a = $(str).find('a').html();

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

Comments

2
  1. make it innerHTML of a temporary div.
  2. use getElementsByTagName("A") to retreive all "A" nodes.
  3. get their HTML .

Here is a running example : http://jsfiddle.net/3fZch/

var str = '<div id="xyz"><p>This is a paragraph</p><a href="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=brd&FlightID=2997227&Page=&PluID=0&Pos=9088" target="_blank"><img src="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=bsr&FlightID=2997227&Page=&PluID=0&Pos=9088" border=0 width=300 height=250></a></div>';


var newElem = returnTheParentNode(str);
var anchors = newElem.getElementsByTagName('A');
/* anchors has all the a tags of the html string */
    for(var i = 0 ; i < anchors.length ; i++)
    {
      var aHTML = getHTML(anchors[i]);
      alert(aHTML);   
    }




function returnTheParentNode(htmlStr)
{
var myCont = document.createElement('DIV'); // create a div element
myCont.innerHTML = htmlStr; // create its children with the string
return myCont;  // return the parent div

}

function getHTML(theNode)
{
    var myCont = document.createElement('DIV');
    myCont.insertBefore(theNode,null);
    return myCont.innerHTML ;
}

4 Comments

Better make this temporary div invisible (with display:none for instance) too !
+1 but perhaps didn't qutie finish the job. If the OP wants the A elements as HTML, then put each A into a temp div and get the div's innerHTML. Don't use regular expressions, they'll probably fail in some cases.
@Creadiff - no need if the div is never added to the document.
Indeed, sorry about the mistake.
0

The expression you need is:

str.match(/a href="([^"]*)"/)[1]

But this assumes there is only one a tag in your string and you used double quotes to delimit the href.

Made a jsfiddle: http://jsfiddle.net/eDAuv/

3 Comments

Also depends on the href appearing as the first attribute - there is no guarantee of that.
I need the whole outerHTML of the <a> block
Rob, it's ok. The string will only ever contain one <a> block.
0

Why not extract the link BEFORE you store it in a string?

myLink = $(".myDiv a").html() 

Comments

0

This could work:

var link = $(str).find("a").get(0).outerHTML;
alert(link);

1 Comment

Is there a way to remove EVERYTHING except the hyperlink block? In case the string is different each time.

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.