unescape-unicode
unescape-unicode is a Node.js package for converting Unicode escapes ("\uxxxx" notation) into their corresponding Unicode characters.
Install
Install using npm:
npm install --save unescape-unicode
Usage
unescapeUnicode(input[, options])
Converts Unicode escapes within input
to their corresponding characters.
Characters that are not part of Unicode escapes are included in the returned string as-is. This includes invalid or
incomplete Unicode escapes (e.g. \uxxxx
) and any other unrelated escape sequences (e.g. \t
). This can be controlled
by specifying the replacer
option.
Characters within the Basic Multilingual Plane (BMP) as well as surrogate pairs for characters outside BMP are supported.
Options
Option | Type | Default | Description |
---|---|---|---|
replacer |
Replacer |
None | A function that returns a replacement string for an individual escape character represented by a specific Unicode code point, if any. |
Examples
import { replaceChars, unescapeUnicode } from "unescape-unicode";
unescapeUnicode("I \\u2665 Unicode!");
//=> "I ♥ Unicode!"
unescapeUnicode("I\\t\\u2665\tUnicode!", { replacer: replaceChars({ f: "\f", n: "\n", r: "\r", t: "\t" }) });
//=> "I ♥ Unicode!"
unescapeUnicode("\\ud842\\udfb7\\ud842\\udfbe");
//=> "𠮷𠮾"
Replacer(code, char)
A function that returns a replacement string for an individual escape character (i.e. the character immediately following a backslash character) represented by the specified Unicode code point, if any.
If an empty string is returned, the escape sequence will be removed from the returned string. If either null
or
undefined
are returned, the escape sequence will be included in the returned string as-is.
This function is only passed u
if not part of a valid Unicode escape sequence.
There are a several built-in Replacer
functions provided.
composeReplacer(...replacers)
Returns a Replacer
composed of the specified replacers
that returns the replacement string returned from the first
Replacer
to return a string, where possible.
import { composeReplacer, replaceChars, replaceUnescaped, unescapeUnicode } from "unescape-unicode";
const replacer = composeReplacer(
replaceChars({ f: "\f", n: "\n", r: "\r", t: "\t" }),
replaceUnescaped(),
);
unescapeUnicode("I\\t\\u2665\\tUnicode\\\\!", { replacer });
//=> "I ♥ Unicode\\!"
replaceConstant(replacement)
Returns a Replacer
that always returns the specified replacement
.
import { replaceConstant, unescapeUnicode } from "unescape-unicode";
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceConstant("?") });
//=> "I?♥?Unicode!"
replaceChar(char, replacement)
Returns a Replacer
that returns the specified replacement
string for the individual escape character (i.e. the
character immediately following a backslash character) provided.
import { replaceChar, unescapeUnicode } from "unescape-unicode";
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceChar("t", "\t") });
//=> "I ♥ Unicode!"
replaceChars(replacements)
Returns a Replacer
that returns replacement strings looked up from the specified replacements
, where possible.
The keys within replacements
are expected to be the individual escape characters (i.e. the character immediately
following a backslash character). It can either be provided as a Map
or an object.
import { replaceChars, unescapeUnicode } from "unescape-unicode";
const replacements = {
f: "\f",
n: "\n",
r: "\r",
t: "\t",
};
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceChars(replacements) });
//=> "I ♥ Unicode!"
replaceCode(code, replacement)
Returns a Replacer
that returns the specified replacement
string for the Unicode code point representing the
individual escape character (i.e. the character immediately following a backslash character) provided.
import { replaceCode, unescapeUnicode } from "unescape-unicode";
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceCode(0x0074, "\t") });
//=> "I ♥ Unicode!"
replaceCodes(replacements)
Returns a Replacer
that returns replacement strings looked up from the specified replacements
, where possible.
The keys within replacements
are expected to be Unicode code points representing the individual escape characters
(i.e. the character immediately following a backslash character). It can either be provided as a Map
or an object.
import { replaceCodes, unescapeUnicode } from "unescape-unicode";
const replacements = new Map([
[0x0066, "\f"],
[0x006e, "\n"],
[0x0072, "\r"],
[0x0074, "\t"],
]);
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceCodes(replacements) });
//=> "I ♥ Unicode!"
replaceUnescaped()
Returns a Replacer
that always returns the individual escape character (i.e. the character immediately following a
backslash character) as the replacement string.
import { replaceUnescaped, unescapeUnicode } from "unescape-unicode";
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceUnescaped() });
//=> "It♥tUnicode!"
Related
Bugs
If you have any problems with this package or would like to see changes currently in development, you can do so here.
Contributors
If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!
A list of all contributors can be found in AUTHORS.md.
License
Copyright © 2025 neocotic
See LICENSE.md for more information on our MIT license.