TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Find HTTP:// and create link (http://www.talkphp.com/javascript-ajax-e4x/3757-find-http-create-link.html)

buildakicker 12-11-2008 11:44 PM

Find HTTP:// and create link
 
Hello all.

I would like to search a DIV and find the static, nonlinked,
http://www.website.com inside the DIV and make it an active. Is there a simple way to do this? I know I can search the div somehow, but how do I replace?

Thanks!

Wildhoney 12-12-2008 02:48 AM

1 Attachment(s)
Try the following code:

javascript Code:
function TalkPHP_MakeLinks(pWithinElement)
{
    if (!pWithinElement)
    {
        return false;
    }
   
    var szContent = pWithinElement.innerHTML;
    pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}

buildakicker 12-12-2008 02:58 PM

Quote:

Originally Posted by Wildhoney (Post 20460)
Try the following code:

javascript Code:
function TalkPHP_MakeLinks(pWithinElement)
{
    if (!pWithinElement)
    {
        return false;
    }
   
    var szContent = pWithinElement.innerHTML;
    pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}

Thanks for the response, works perfect. I am not sure I understand how it is working however. Are these comments correct:

function TalkPHP_MakeLinks(pWithinElement)
{
//if there is no DIV with this name, do nothing.
if (!pWithinElement)
{
return false;
}

//this var equals the DIV to be "edited"
var szContent = pWithinElement.innerHTML;
//this finds the HTTP and makes it a link...
pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}

Can you explain this:

http:\/\/.+?)[\s|\n]/ig


Thanks again!^^

Salathe 12-13-2008 12:17 AM

/(http:\/\/.+?)[\s|\n]/ig creates a regular expression object with the pattern (http:\/\/.+?)[\s|\n] and flags i (case insensitive) and g (global).

The pattern itself searches for (and retains for later) the characters "http://" followed by one or more (+) single characters (.; 'single character' means any character except a newline). The question mark (?) makes the 'one or more single characters' what is called ungreedy (which is important but we can gloss over it right now). Next, [\s|\n] matches either a whitespace character (tab, space, newline), a vertical pipe character or a newline character (I think Wildhoney really meant just \s so that the match stops at whitespace (thanks to the ungreedy repetition earlier)).

Put simply, we match all instances of "http://" followed by one or more characters, followed by a whitespace character.

Wildhoney 12-13-2008 12:47 AM

Thanks Salathe. Yes, I did mean that. Either a space or a line break, but it would seem a space in this context behaves as a new line as well. Why is this? I thought it would look for a space, and so I had to specify either a space or a new line.

The following regular expression appears to work just fine, even if the link appears at the end of a line followed by a line break:

Code:

/(http:\/\/.+?)\s/ig

Salathe 12-13-2008 01:06 AM

The \s escape sequence matches any whitespace character which includes the space character along with such beauties as the tab and newline characters.


All times are GMT. The time now is 06:20 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0