Change url to clickable link by javascript

Discussion in 'JavaScript' started by namln, Nov 19, 2009.

  1. #1
    Hi there,
    I want to chage all url in my web to clickable automatically.


    I found a php code to do it:
    document.writeln("  $text = eregi_replace('([[:space:]()[{}])?(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)([[:space:]()[{}])?', ' <a href=\"2\" target=\"_blank\">2</a> ', $text); "); 
    document.writeln(" $text = eregi_replace('([[:space:]()[{}])?(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)([[:space:]()[{}])?', '1 <a href=\"http://2\" target=\"_blank\">2</a> ', $text); "); 
    document.writeln(" $text = eregi_replace('([[:space:]()[{}])?(file:(/)?[-a-zA-Z0-9@:%_+.~#?&//=]+)([[:space:]()[{}])?', '1 <a href=\"2\">2</a> ', $text); "); 
    document.writeln(" $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})', '<a href=\"mailto:1\">2</a>', $text);   ");
    PHP:
    Can anyone convert it to javascript or give me a javascript to do it?

    Ex:
    URL:
    http://forums.digitalpoint.com
    Code (markup):
    To clickable link: http://forums.digitalpoint.com
    Please help me do this. Thanks very much!
     
    Last edited: Nov 19, 2009
    namln, Nov 19, 2009 IP
  2. robbin.joe

    robbin.joe Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use this regex in javascript

    /(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/ig
     
    robbin.joe, Nov 19, 2009 IP
  3. namln

    namln Active Member

    Messages:
    280
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Can you explain how can I do it, please!
    Thank you.
     
    namln, Nov 19, 2009 IP
  4. robbin.joe

    robbin.joe Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    var str = 'http://forums.digitalpoint.com/';
    var newstr = str.replace(/(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/ig, 
     function(){
      var link = arguments[0];
      if(link) {
        return '<a href="'+link +'">' + link +'</a>';
      }
     }
    );
    Code (markup):
    sorry, a bit late.
     
    robbin.joe, Nov 20, 2009 IP