Skip to content

Instantly share code, notes, and snippets.

@vjt
Created August 6, 2010 19:27
Show Gist options
  • Save vjt/511848 to your computer and use it in GitHub Desktop.
Save vjt/511848 to your computer and use it in GitHub Desktop.
/**
* Usage:
*
* js> "some:string.you?wanna-escape".escapeChars(/[:\.\?-]/g);
* ==> "some%3Astring%2Eyou%3Fwanna%2Descape"
*
* The full power of regexes is in your hands, use it for good!
*
* - [email protected]
* Fri Aug 6 21:26:20 CEST 2010
*
*/
String.prototype.escapeChars = function (re) {
return this.replace(re, function(char, i) {
var n = char.charCodeAt(0), s = '';
do {
s = "0123456789ABCDEF"[n&15] + s;
n = n >> 4;
} while (n);
return '%' + s;
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment