Okay, now that I've gone thru all of that, I now believe I know your intent and your solution is way too complex. If the intent is that only urls which match ```from/...``` should be converted (e.g., ```/static/mypicture.png``` matches ```/static/``` but ```http:www.mysite.mysub.org/static/mypicture.png``` should not), then the solution is 
``` javascript
function foo(content, from, to) {
    if (from === null || to === null) {
        return content;
    }
    // handle http and https
    // note: add other protocols here
    var regex = new RegExp("^"+from, 'g');
    return content.replace(regex, to);
}
```