Skip to content Skip to sidebar Skip to footer

Regex To Find Urls Not In Tags

I am breaking my head on this: I am trying to find URLs in Javascript with regex. Update: I use Javascript on serverside, so I can not walk through the DOM (http:\/\/|https:\/\/|)(

Solution 1:

It would be simpler if you allowed a non-href url to be the text of an a element. As you require, you need to avoid any child nodes of the a elements, in case you have an url like text in a span or strong or whatever child of an a.

functionsomeurls(node){
        var A= [], tem, rx=/^https?\:\/\/[^\s]+/g;
        if(node){
            node= node.firstChild;
            while(node && node.tagName== 'A') node= node.nextSibling;
            while(node!= null){
                if(node.nodeType== 3){
                    if((tem= node.data.match(rx))!= null) A[A.length]= tem;
                }
                else A= A.concat(someurls(node));
                node= node.nextSibling;
                while(node && node.tagName== 'A') node= node.nextSibling;
            }
        }
        return A;
    }

// alert(someurls(document.body).join('\n')

Post a Comment for "Regex To Find Urls Not In Tags"