title: Exact match in Find and Replace script
body: <p>I'm running into a problem with this script where it's replacing all text. <a href="https://i.imgur.com/QKLf4nM.png" rel="nofollow noreferrer">See here</a>. The text inside the cells is either =#ERROR! or =#NAME? and the error is coming back as Unknown range name. How can I get it to only replace the specific string of text? Just T, A, R, etc. and not <em>every</em> word that contains those letters.</p>
  
  <pre><code>function runReplaceInSheet(){
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Underlevel");
    //  get the current data range values as an array
    //  Fewer calls to access the sheet -&gt; lower overhead 
    var values = sheet.getDataRange().getValues();  
  
  var range = sheet.getDataRange();
  var replaceObj = {
    //to_replace: imgur id
    T: 'Dxl893F',
    A: 'omc7F9l',
    R: '12ZmSp3',
    M: 'kh7RqBD',
    H: 'u0O7fsS',
    F: 'Hbs3TuP',
  };
  var regex = new RegExp('^(' + Object.keys(replaceObj).join('|') + ')$', 'g');// /^(T|A|R|M|H|F)$/
  function replacer(match) {
    return '=image("https://i.imgur.com/' + replaceObj[match] + '.png")';
  }
  var data = range.getValues();
  range.setFormulas(
    range.getFormulas().map(function(e, i) {//i=index of row(e)
      return e.map(function(f, j) {//j = index of column(f)
        return f === "" ? data[i][j].toString().replace(regex, replacer) : f;
      });
    })
  );
  }
  </code></pre>
  
  <p>@Tanaike</p>