Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheets: Any Regex Replace

I currently use

SUBSTITUTE(text, search_text, new text, occurrence)

to replace text, but I am starting to find that I have a list of things to replace, the above method will become something like

SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(...)...)...)...

very messy, whats a better method of making this work?

like image 933
JM at Work Avatar asked Dec 28 '25 03:12

JM at Work


1 Answers

You might be able to use a regular expression using the REGEXREPLACE(text, regular_expression, replacement) function instead.

If this doesn't work, it's possible to write custom functions using Google Apps Script. Maybe you could write one that can accept multiple strings to be replaced.

Edit: I just wrote such a function:

function SubstituteAll(text) {
  arguments = Array.prototype.slice.apply(arguments);
  arguments.shift();
  var replacement = arguments.pop();
  for(var i=0; i<arguments.length; i++)
  {
    text = text.replace(arguments[i], replacement);
  }
  return text;
}

Usage:

SubstituteAll(text, search_text1, search_text2, ..., replacement_text)
like image 54
Na7coldwater Avatar answered Dec 30 '25 22:12

Na7coldwater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!