Skip to content

Instantly share code, notes, and snippets.

@naugtur
Created February 22, 2016 10:34
Show Gist options
  • Select an option

  • Save naugtur/1d2227b09601e50dc9e0 to your computer and use it in GitHub Desktop.

Select an option

Save naugtur/1d2227b09601e50dc9e0 to your computer and use it in GitHub Desktop.
string multireplace not regexp
function replaceSubstring(inSource, inToReplace, inReplaceWith) {
var outString = [];
var repLen = inToReplace.length;
var idx = inSource.indexOf(inToReplace);
while (idx !== -1) {
outString.push(inSource.substring(0, idx))
outString.push(inReplaceWith);
inSource = inSource.substring(idx + repLen);
idx = inSource.indexOf(inToReplace);
}
outString.push(inSource);
return outString.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment