Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Created December 9, 2011 12:08
Show Gist options
  • Save gerardpaapu/1451292 to your computer and use it in GitHub Desktop.
Save gerardpaapu/1451292 to your computer and use it in GitHub Desktop.
Get a clean ASCII string from a JavaScript string
function cleanString(str, method, r) {
var result = '',
max = str.length,
i, ch;
for (i = 0; i < max; i++) {
ch = str.charCodeAt(i);
if (ch < 128) {
result += str.charAt(i);
} else {
switch (method) {
case 'elide':
break;
case 'replace':
result += r || '?';
break;
case 'entity':
result += '&#' + ch + ';';
break;
default:
throw new Error();
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment