替换文本中的URL为Link

posted on 2009-09-14 01:41:44+08:00 by david
来源:http://wangblog.org/2009/09/urllink.html

注意:如果文本URL后面有『<』,要过滤,否则出错。

plain2link(obj.innerHTML.replace(/<br[^>]*>/gi, "\n<br />"));

将文本URL后面的『<b />』用换行过滤!
  1. function plain2link(text){
  2. if(!text) return text;
  3.  
  4. text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, function(url){
  5. nice = url;
  6. if(!url.match('^https?:\/\/')){
  7. url = 'http://'+url;
  8. }
  9. return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice +'</a>';
  10. });
  11.  
  12. return text;
  13. }

Comments

posted on 2009-09-14 02:11:28+08:00 by david
var str = obj.innerHTML.replace(/<br[^>]*>/gi, "\n<br />");
str = str.replace(/\&lt\;/gi, "\n\&lt\;");
str = str.replace(/\&#60\;/gi, "\n\&#60\;");
obj.innerHTML = plain2link(str);

All Code Snippets

Hot Code Snippets

New Code Snippets