ThE_-_BliZZarD
ThE_-_BliZZarD

Reputation: 774

Escape with Unicode Code Points similar to PHP in Javascript

When using PHP to generate JSON, it encodes higher characters using the \u0123 code-point notation.

(I know this not necessary, but for unnamed reasons I want that.)

I am trying to achieve the same in JavaScript. I searched high and low and found nothing. The encodeUri function does nothing for me (even though many suggested that it would).

Any helpful hints? I hope that I do not have to use some big external library but rather something build-in or a few lines of nice code - this can not be this hard, can it...?!

I have an input string in the form of:

var stringVar = 'Hällö Würld.';

My desired conversion would give me something like:

var resultStringVar = 'H\u00e4ll\u00f6 W\u00fcrld.';

Upvotes: 1

Views: 98

Answers (1)

Mathias Bynens
Mathias Bynens

Reputation: 149534

I’ve made a library for just that called jsesc. From its README:

This is a JavaScript library for escaping JavaScript strings while generating the shortest possible valid ASCII-only output. Here’s an online demo.

This can be used to avoid mojibake and other encoding issues, or even to avoid errors when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or lone surrogates) to a JavaScript parser or an UTF-8 encoder, respectively.

For your specific example, use it as follows:

var stringVar = 'Hällö Würld.';
var resultStringVar = jsesc(stringVar, { 'json': true, 'wrap': false });

Upvotes: 2

Related Questions