Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  150] [ 1]  / answers: 1 / hits: 19433  / 15 Years ago, wed, november 18, 2009, 12:00:00

I need a Javascript regular expression that scans a block of plain text and returns the text with the URLs as links.



This is what i have:




findLinks: function(s) {
var hlink = /s(ht|f)tp://([^ ,;:!)(\'\fnrtv])+/g;
return (s.replace(hlink, function($0, $1, $2) {
s = $0.substring(1, $0.length);
while (s.length > 0 && s.charAt(s.length - 1) == '.') s = s.substring(0, s.length - 1);

return ' ' + s + '';
}));
}


the problem is that it will only match http://www.google.com
and NOT google.com/adsense



How could I accomplish both?


More From » regex

 Answers
10

I use this a as reference all the time. This guy has 8 regex's you should know.



http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/



Here is what he uses to look for URL's



/^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$/ 


He also breaks down what each part does. Very useful for learning regex's and not just getting an answer that works for reasons you don't understand.


[#98285] Saturday, November 14, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alonso

Total Points: 747
Total Questions: 108
Total Answers: 105

Location: Mauritania
Member since Sun, Sep 25, 2022
2 Years ago
;