Advertisement
stuppid_bot

Escape Unicode Characters

Sep 4th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function escapeUnicode(s) {
  2.     return s.replace(/[^\x20-\x7e]/g, function (c) {
  3.         c = c.charCodeAt(0);
  4.         if (c == 8) c = '\\b';
  5.         else if (c == 9) c = '\\t';
  6.         else if (c == 10) c = '\\n';
  7.         else if (c == 12) c = '\\f';
  8.         else if (c == 13) c = '\\r';
  9.         else if (c < 128) c = '\\x' + (c < 16 ? '0' : '') + c.toString(16);
  10.         else c = c.toString(16), c = '\\u' + ('000' + c).slice(-4);
  11.         return c;
  12.     });
  13. }
  14.  
  15. console.log(escapeUnicode('吉 – удача.')); // "\u5409 \u2013 \u0443\u0434\u0430\u0447\u0430."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement