<p>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.</p>

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

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

Replacing URLs with links (Answer to the General Problem)
The regular expression in the question misses
a lot
of edge cases. When detecting URLs, it's always better to use a specialized library that handles international domain names, new TLDs like
```
.museum
```
, parentheses and other punctuation within and at the end of the URL, and many other edge cases. See the Jeff Atwood's blog post
The Problem With URLs
for an explanation of some of the other issues.
The
best summary of URL matching libraries
is in
Dan Dascalescu's Answer
(as of Feb 2014)
"Make a regular expression replace more than one match" (Answer to the specific problem)
Add a "g" to the end of the regular expression to enable global matching:
```
```
/ig;
```
```
But that only fixes the problem in the question where the regular expression was only replacing the first match.
Do not use that code.