Skip to content

Instantly share code, notes, and snippets.

@brito
Created April 2, 2021 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brito/df7d8c831858352f3182c195b5bceffc to your computer and use it in GitHub Desktop.
Save brito/df7d8c831858352f3182c195b5bceffc to your computer and use it in GitHub Desktop.
Transform a string using multiple regex
/*
Transform a string using multiple regex
@customfunction
*/
function replaces(operators,operands) {
return operators[0].map ?
operators.reduce((a,b) => replaces(b,a), operands)
: operands.replace(new RegExp(operators[0]), operators[1])
}
function __TESTS(){ __TEST_0_replaces(), __TEST_1_replaces()}
function __TEST_0_replaces(){ Logger.log(replaces(['hello', 'tests'],'hello world')) }
function __TEST_1_replaces(){ Logger.log(replaces([['hello', 'tests'],['world','pass']],'hello world')) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment