-1

I have the following php code to detect and replace links:

//Detect links
    $pattern_url='~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
    preg_match_all($pattern_url, $post, $matches); 
    for ($i=0; $i < count($matches[0]); $i++)
    {
        if(substr($matches[0][$i],0,4)=='www.')
        $post = str_replace($matches[0][$i],'http://'.$matches[0][$i],$post);
    }
    $post = preg_replace($pattern_url,'<a target="_blank" href="\\0">\\0</a>', $post);

I would like to have the same in javascript/jquery.

How is that possible?

kapa
  • 77,694
  • 21
  • 158
  • 175
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115

1 Answers1

0
function replaceURLWithHTMLLinks(text) {
   var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
   return text.replace(exp,"<a href='$1'>$1</a>"); 
}

How to replace plain URLs with links?

Community
  • 1
  • 1
semsem
  • 1,194
  • 12
  • 24