Skip to content

Instantly share code, notes, and snippets.

@sauntimo
Last active December 13, 2018 16:40
Show Gist options
  • Save sauntimo/3e837e59b457e1c5ec5df247a1d029b8 to your computer and use it in GitHub Desktop.
Save sauntimo/3e837e59b457e1c5ec5df247a1d029b8 to your computer and use it in GitHub Desktop.
Ascii encode dodgy characters in a given string
/**
* 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