Javascript find and replace works with string but not variable
<p>Hoping someone can help me it's driving me nuts.</p>

<p>I am using SPServices and Javascript to rewrite some links after a small content migration.</p>

<p>I have some HTML in a variable and I'm trying to find a string which is a specific URL and replace it with a different URL. This works:</p>

<p><code>newHTML = newHTML.replace("http://urlhere/subsite/page.aspx",newLink);</code> </p>

<p>Also this works: </p>

<pre><code>newHTML = newHTML.replace(new RegExp("http://urlhere/subsite/page.aspx", 'gi'), newLink); 
</code></pre>

<p>But if I have a variable containing the same string in there it doesn't work:</p>

<pre><code>newHTML = newHTML.replace(new RegExp(oldLink, 'gi'), newLink); 
</code></pre>

<p>My oldLink variable comes from an SPServices call to another list column containing HTML, which I take the 'a' tags and put them into an array:</p>

<pre><code>function rewriteLinks() {
var urlStart = "http://urlstart";



var linkContainerArray = [];


    var theContent = $('.htmlContentDiv').html();

    // get a tags
    var aTags = ('a',theContent);
    //loop through A tags and get oldLink and Text
    $(aTags).each(function(){

    var ID;

    var itemTitle = $(this).text();

    var oldLink = $(this).attr('href');

    var newLink;

    if(itemTitle.length &gt; 2){
        //SpService call to get ID of that item using inner text as query to SharePoint list

            $().SPServices({
            operation: "GetListItems",
            async: false,
            CAMLViewFields: "&lt;ViewFields&gt;&lt;FieldRef Name='Title' /&gt;&lt;FieldRef Name='ID' /&gt;&lt;/ViewFields&gt;",
            CAMLQuery: '&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name="Title" /&gt;&lt;Value Type="Text"&gt;&lt;![CDATA['+itemTitle+']]&gt;&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;',
            listName: 'AGItems',
            completefunc: function (xData, Status) {
                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                    ID = $(this).attr("ows_ID");  
                     //Now have oldLink and newID in variables - build newLink from known URL &amp; newID
                     newLink = urlStart+ID;     
                        });//response xml
                    }//completefunc
                });//spservices 
    //check for empty links
    if((oldLink &amp;&amp; newLink != '') &amp;&amp; (oldLink &amp;&amp; newLink != undefined)){
        var linkPair = [oldLink,newLink];
        linkContainerArray.push(linkPair);
    }


    }  

   });

    replaceLinks(linkContainerArray);   


}
</code></pre>

<p>Then I call a function to find and replace the links (this is where my variable won't work). I've tried escaping in all combinations of the following ways:</p>

<pre><code>function escapeRegExp(stringToGoIntoTheRegex) {
return stringToGoIntoTheRegex.replace(/[\/\\^$*+?.|[\]{}]/g, '\\$&amp;');
}

function htmlEncode(value){
//create a in-memory div, set it's inner text(which jQuery     automatically        encodes)
  //then grab the encoded contents back out.  The div never exists on the     page.
  return $('&lt;div/&gt;').text(value).html();
}

function htmlEscape(str) {
return String(str)
    .replace(/"/g, '&amp;quot;')
    .replace(/'/g, "&amp;#39;")
    .replace(/&lt;/g, '&amp;lt;')
    .replace(/&gt;/g, '&amp;gt;');
 }

 function escapeRegExp(stringToGoIntoTheRegex) {
    return stringToGoIntoTheRegex.replace(/[\/\\^$*+?.|[\]{}]/g, '\\$&amp;');
}
</code></pre>

<p>Also removed full stops and question marks from the HTML &amp; variable to make everything simple and it's still not working. </p>

<p>Also tried encodeURIComponent on the HTML &amp; oldlink variable.. still no luck</p>

<p>If anyone has any help for me with this at all it would be much appreciated or can maybe see what I'm missing?!</p>

<p>Thanks</p>