Removes a given escape sequence from before characters in a string. Correctly handles double-escapes by only unescaping them once.
Requires Node.js 8 or above.
npm i char-unescape
The module exports a single function.
Optional: escapeChar
(string): The escape sequence to remove. Defaults to \
.
Returns a function which accepts a single parameter: a string to which the unescape operation should be applied.
// Note the function call!
// No arguments are passed here because we're using
// the default backslash escape character.
const charUnescape = require('char-unescape')()
charUnescape("This is \\\\a \\'test\\'") // "This is \\a 'test'"
const charUnescape = require('char-unescape')('@')
charUnescape("This is @@a @'test@'") // "This is @a 'test'"