title: Limit Find and Replace to whole words google sheets .gs function
body: <p>I have a find and replace function, it replaces <code>every</code> instance of <code>aa</code> in a string, in VBA it would be <code>xlPart</code></p>
  
  <p>Is there a way to set the function so that it replaces <code>only</code> whole words or in VBA language <code>xlWhole</code></p>
  
  <p>Thanks</p>
  
  <pre><code>function FindReplaceWSHT(){
  
      replaceInSheet("Elements",'aa','ZZZ');
  }
  
  function replaceInSheet(shtName, to_replace, replace_with) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName);
  
  //get the current data range values as an array
  var values = sheet.getDataRange().getValues();
  
  //loop over the rows in the array
  for(var row in values){
  
  //use Array.map to execute a replace call on each of the cells in the row.
  var replaced_values = values[row].map(function(original_value){
    return original_value.toString().replace(to_replace,replace_with);
  });
  
  //replace the original row values with the replaced values
  values[row] = replaced_values;
  }
  
  //write the updated values to the sheet
  sheet.getDataRange().setValues(values);
  }
  </code></pre>