![]() |
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! |
1 Attachment(s)
Try the following code:
javascript Code:
|
Quote:
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!^^ |
/(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. |
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 |
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