How to replace plain URLs with links?

Posted by Sergio del Amo on Stack Overflow See other posts from Stack Overflow or by Sergio del Amo
Published on 2008-09-01T09:58:49Z Indexed on 2012/11/22 23:00 UTC
Read the original article Hit count: 186

Filed under:
|

I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing the first match.

How I can replace all the URL? I guess I should be using the exec command, but I did not really figure how to do it.

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

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex