6

I am trying to write javascript code to find all the urls inside a div. Now this would be pretty easy if all the urls within the div were separated by spaces in which case I can just do a regex on what's inside the div to find them. However, the urls within this outer div may be in sub divs (or any other html tag) and I want to consider the subdivs as separators as well (and I don't want to get rid of these subdivs). To give an example, in the following I want to find www.foo.com and www.bar.com within the div with id "outer":

<div id="outer"><div>www.foo.com</div>www.bar.com</div>

What would be a good way of doing this?

shoopdelang
  • 985
  • 2
  • 9
  • 20

3 Answers3

3

You can apply a recursive call to all non-text child nodes.

function replaceWwwInNodes(node) {
    //text node
    if (node.nodeType === 3) {
        node.textContent = node.textContent.replace(/* ??? */)
    }
    else {
        Array.prototype.forEach.call(node.childNodes, function (elem) {
            replaceWwwInNodes(elem);
        });
    }
}

replaceWwwInNodes(document.getElementById('outer'));

http://jsfiddle.net/UDX5V/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Try to use this sample http://jsfiddle.net/iklementiev/TaCx9/1/

var data = document.getElementById("outer").innerText;
var myRe = /www\.[0-9a-z-]+\.[a-z]{2,4}/igm;
var matches=  data.match(myRe)

for (var i = 0; i < matches.length; i++) {
    alert('match: ' + matches[i]);
}

this help to find all urls.

Ilya Klementiev
  • 593
  • 8
  • 12
0

try this

var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
 var regex = new RegExp(expression);
var regContent = $("#outer").html();
var newContent = regContent;                                                                     
if(regContent.match(regex))
{    
    var textContent = regContent.match(regex);                                                                         
for(var i=0;i<regContent.match(regex).length;i++)
{
    newContent = newContent.replace(new RegExp(regContent.match(regex)[i], "g"), "test");
}    
$("#outer").html(newContent);
}   

this will get all url content and replace it as "test".

Mathi
  • 742
  • 1
  • 10
  • 15