Skip to content

Instantly share code, notes, and snippets.

@pdscopes
Created September 13, 2018 10:56
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 pdscopes/3589a6d8151ac42a6ea26687b6c0c71e to your computer and use it in GitHub Desktop.
Save pdscopes/3589a6d8151ac42a6ea26687b6c0c71e to your computer and use it in GitHub Desktop.
String.prototype.replace_nth
String.prototype.replace_nth = function (searchValue, replaceValue, n) {
let i = 0;
return this.replace(searchValue, function (match) { return (++i === n) ? replaceValue : match; });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment