× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Escape Node.js JSON encoding like in PHP

Alexander Fedra
Oct 20, 2014
<p>PHP Example:</p><pre><code class="language-php">json_encode('ABC-ÄÖÜ');</code></pre><p>will return "ABC-\u00c4\u00d6\u00dc"<br><br>Node.js Example:</p><pre><code class="language-javascript">JSON.stringify('ABC-ÄÖÜ')</code></pre><p>will return "ABC-ÄÖÜ"<br><br>Node.js with extra encoding:</p><pre><code class="language-javascript">JSON.stringify('ABC-ÄÖÜ').replace(/[\u0080-\uFFFF]/g, function(match) { return '\\u' + ('0000' + match.charCodeAt(0).toString(16)).slice(-4); });</code></pre><p>will also return "ABC-\u00c4\u00d6\u00dc" like PHP</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus