Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created August 24, 2012 14:09
Show Gist options
  • Save oberhamsi/3451008 to your computer and use it in GitHub Desktop.
Save oberhamsi/3451008 to your computer and use it in GitHub Desktop.
fix unicode string
/* call with file to be cleaned
write result into file with postfix ".cleaned"
*/
var fs = require('fs');
var system = require('system');
var text = fs.read(system.args[1]);
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
if (cx.test(text)) {
text = text.replace(cx, function(a) {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
});
}
fs.write(system.args[0] + '.cleaned', text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment