How to replace plain URLs with links, in javascript?
I think that it's nutty to replace all characters 'a' and 'b' with junk, then un-replace them later, just so that you can use 'a' and 'b' as landmarks to indicate which points your two regular expressions have in common.  If you're going to do that, then why not just use "--ucsps--" and "--uspds--" as your landmarks in the first place?  Of course, using any special strings as landmarks is a bad idea.

The fundamental problem is that you want to exclude the same number of closing parentheses as there are open parentheses.  You can't use a regular expression to match left and right parentheses, because such an expression would not be regular.  Well, some flavours of "regular" expressions support backreferences within the expression itself, which could do the job.  RegExps in JavaScript don't.

What JavaScript can do, though, is let you specify your own code to generate the replacement text.  Here's what I came up with:

function replaceURLWithHTMLLinks(text) {
    var re = /(\(.*?)?\b((?:https?|ftp|file):\/\/[-a-z0-9+&amp;@#\/%?=~_()|!:,.;]*[-a-z0-9+&amp;@#\/%=~_()|])/ig;
    return text.replace(re, function(match, lParens, url) {
        var rParens = '';
        lParens = lParens || '';

        // Try to strip the same number of right parens from url
        // as there are left parens.  Here, lParenCounter must be
        // a RegExp object.  You cannot use a literal
        //     while (/\(/g.exec(lParens)) { ... }
        // because an object is needed to store the lastIndex state.
        var lParenCounter = /\(/g;
        while (lParenCounter.exec(lParens)) {
            var m;
            // We want m[1] to be greedy, unless a period precedes the
            // right parenthesis.  These tests cannot be simplified as
            //     /(.*)(\.?\).*)/.exec(url)
            // because if (.*) is greedy then \.? never gets a chance.
            if (m = /(.*)(\.\).*)/.exec(url) ||
                    /(.*)(\).*)/.exec(url)) {
                url = m[1];
                rParens = m[2] + rParens;
            }
        }
        return lParens + "&lt;a href='" + url + "'&gt;" + url + "&lt;/a&gt;" + rParens;
    });
}