Last active
December 13, 2018 16:40
-
-
Save sauntimo/3e837e59b457e1c5ec5df247a1d029b8 to your computer and use it in GitHub Desktop.
Ascii encode dodgy characters in a given string
This file contains 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
/** | |
* replaces non-db safe characters with ascii escaped sequences | |
* @param {string} string to ascii escape | |
*/ | |
function asciiEncode(string) { | |
// get an array of dodgy characters | |
var arr_replace = string.match(/[^a-z0-9 _-~#;]/ig); | |
// loop over characters to replace if matches returned | |
arr_replace && arr_replace.forEach(function (replace) { | |
var re = new RegExp( replace , 'g'); | |
// replace with ascii encoded character | |
string = string.replace(re, '&#' + replace.charCodeAt(0) + ';'); | |
}); | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment