Created
August 6, 2010 19:27
-
-
Save vjt/511848 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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