0

please help me

 <html>
  <body>
       http://domainname.com/abc/xyz.zip
       http://domainname2.com/abc/xyz.zip 

 </body>
 </html>

I want replace with link and out put like

 <html>
  <body>
      <a href="http://domainname.com/abc/xyz.zip">http://domainname.com/abc/xyz.zip</a>
      <a href="http://domainname2.com/abc/xyz.zip">http://domainname2.com/abc/xyz.zip</a>

 </body>
 </html>

Great Thank

Thoman
  • 742
  • 2
  • 9
  • 20
  • possible duplicate of [replace URL with HTML Links javascript](http://stackoverflow.com/questions/37684/replace-url-with-html-links-javascript) – Skilldrick Jun 15 '10 at 15:35

2 Answers2

1

You should not use regular expressions to parse HTML.

Community
  • 1
  • 1
VeeArr
  • 6,039
  • 3
  • 24
  • 45
0

Try this function:

function linkify(element) {
    var children = element.childNodes,
        stub = document.createDocumentFragment();
    for (var i=0; i<children.length; ++i) {
        if (children[i].nodeType === Node.TEXT_NODE) {
            var parts = children[i].nodeValue.split(/(http:\/\/\S+)/);  // adjust regex
            if (parts.length > 1) {
                for (var j=0; j<parts.length; ++j) {
                    if (j % 2 === 1) {
                        var link = document.createElement("a");
                        link.setAttribute("href", parts[j]);
                        link.appendChild(document.createTextNode(parts[j]));
                        stub.appendChild(link);
                    } else {
                        stub.appendChild(document.createTextNode(parts[j]));
                    }
                }
                continue;
            }
        }
        stub.appendChild(children[i]);
    }
    document.write(stub.childNodes.length);
    element.parentNode.replaceChild(stub, element);
}
linkify(document.body);

This is pure DOM manipulation. This linkify function expects the argument to be a HTMLElement. You just need to adjust the regular expression to grab the URLs.

Gumbo
  • 643,351
  • 109
  • 780
  • 844