Skip to content

Instantly share code, notes, and snippets.

@st98
Created March 23, 2014 10:05
Show Gist options
  • Save st98/9721034 to your computer and use it in GitHub Desktop.
Save st98/9721034 to your computer and use it in GitHub Desktop.
'ABC'.tr('ABC', 'DEF'); // => 'DEF'
String.prototype.tr = function (search, replace) {
var pattern = new RegExp('[' + search + ']', 'g');
return this.replace(pattern, function (m) {
return replace[search.indexOf(m)] || '';
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment