2

If I have a string, for example: abcℂ, then how do I escape the non-ASCII characters? Here is the desired behavior:

escapeUnicode("abcℂ")  →  "abc\ud835\udd38\ud835\udd39\u2102"
vageko4924
  • 111
  • 4
  • Please take time to search for your problem before asking a question. https://stackoverflow.com/questions/7499473/need-to-escape-non-ascii-characters-in-javascript – Sam Walpole Jun 18 '20 at 11:39
  • 1
    @SamWalpole Ah, you're right. I did try a few Google searches and only found python and PHP variants. I already had the JS answer so I figured I'd make a SO question on it and answer my own question so it was easier for future searchers to find. Also, interesting that SO didn't find that question in the "related questions" box that you get before submitting a question, since the titles are very similar. – vageko4924 Jun 19 '20 at 07:22

1 Answers1

3

This function matches all non-ASCII characters after splitting the string in a "unicode-safe" way (using [...str]). It then splits each unicode character up into its code-points, and gets the escape code for each, and then joins all the ASCII characters and Unicode escapes into one string.

function escapeUnicode(str) {
   return [...str].map(c => /^[\x00-\x7F]$/.test(c) ? c : c.split("").map(a => "\\u" + a.charCodeAt().toString(16).padStart(4, "0")).join("")).join("");
}
vageko4924
  • 111
  • 4