Package detail

unescape-unicode

neocotic2.6kMIT0.3.0

Library to unescape Unicode characters

converter, unicode, unescape

readme

unescape-unicode

Build Status Downloads Release License

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.

changelog

Version 0.3.0, 2025.08.04

  • Breaking Change: Convert Unicode escapes within input parameter to their corresponding characters
    • Characters that are not part of Unicode escapes are included in the returned string as-is, including invalid or incomplete Unicode escapes (e.g. \uxxxx)
  • Breaking Change: No longer throw an error if no valid Unicode escape is found in input parameter
  • Breaking Change: Remove start 2nd optional parameter and always unescape all valid Unicode escapes within the full range of input parameter
    • Caller is now responsible for slicing the input parameter beforehand, if desired
  • Breaking Change: Add optional options 2nd parameter to support the following:
    • replacer - A function that returns a replacement string for an individual escape character represented by a specific Unicode code point, if any
  • Breaking Change: Return empty string if input parameter is either null or undefined
  • Explicitly add full support for converting characters within the Basic Multilingual Plane (BMP)
  • Rewrite the entire codebase in TypeScript and support both ESM and CJS usage
  • Improve documentation
  • Improve the developer experience for contributors with better tooling
  • Bump all dependencies to latest versions

Version 0.2.0, 2018.11.09

  • added package-lock.json file to enable "npm audit" 9bd6510
  • moved from !ninja to neocotic a570131
  • modified CI to now target Node.js 8, 10, and 11 0315b60
  • bumped devDependencies 6808784

Version 0.1.0, 2018.01.25

  • Initial release