views:

158

answers:

2

I'm currently using the following code, to search the a div on a page for URLs and replace them with a tags.

However when we have a embed tags within the div it messes up the links within these tags.

function replaceURLWithHTMLLinks(text) { 
    return text.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,'<a href="$&" class="my_link" target="_blank">$&</a>').replace(/([^\/])(www[^ <]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>');
}

$(document).ready(function(){
    var htmlStr = $("div.content-a").html();
    var htmlStrAfter = replaceURLWithHTMLLinks(htmlStr);
    $("div.content-a").html(htmlStrAfter);
});

Can anyone tell be how to perhaps exclude any http://... that are preceded by a " or ' ?

Or similar?

A: 
prodigitalson
I can't seem to get this to work even once I changed 'div.content' to 'div.content-a'.This div will normally contain text and a couple of more divs.However it also contains a table with an embed tag within it. And an img tag with a URL in the alt (quirk of our system).
Stephen Keable
Well then it must not work at all id assume because unless your selector doesnt match anything there should be no difference... What happens do you get any errors?
prodigitalson
BTW - i jsut used your regex - i assumed that worked except for catching urls in tags my code only attempts to miss the tags - if the regex itself doesnt work then perhaps reformulate it Rorick has some good suggestions from what i can tell but im no regex-ninja :-)
prodigitalson
The regex worked, however with your code it seems to filter all tags. The content within the div is not contained within any <p> tags just straight text with an optional inline embed tag.
Stephen Keable
That shouldnt do anything to the tags... `contents()` will get all children of the selector including text nodes (the text nodes are what you want) then that if statement should test that the node is a text node (nodeType = 3 = text)... or at least it should... i changed it to use jquery's filter method instead see if that makes a difference.
prodigitalson
A: 

Probably, you should use DOM as recommended. But in order to make your regexp work as desired, you should prepend it with (?:^|[^"']). That means match start of line or match any character excepting ' and ". So first of your regexps will look as follows:

/(?:^|[^"'])(ftp|http|https|file):\/\/[\S]+(\b|$)/gim  

And your chaining of replace method is ugly. Code will be much more readable if you will split method invocations to different lines.

Update: And in order to skip first excess character you can use $1 instead of $& and your regexp must be changed to this:

/(?:^|[^"'])((ftp|http|https|file):\/\/[\S]+(\b|$))/gim  
Rorick