Skip to content

@technobuddha > library > Programming > Escaping

Function: escapeJS()

ts
function escapeJS(input: string): string;

Defined in: escape-js.ts:38

Escape a string for use in Javascript

CharacterHexEscape Sequence
NUL0x00\0 or \x00[^1]
Backspace0x08\b
Tab0x09\t
Newline0x0a\n
Vertical Tab0x0b\v
Form Feed0x0c\f
Carriage Return0x0d\r
Double Quote0x22\"
Single Quote0x27\'
Backslash0x5c\\
Control Characters0x00-0x1f, 0x7f-0x9f\xnn
BMP0x0100-0xffff\unnnn
Astral0x10000-0x10ffff\u

[^1]: The sequence \0 must not be followed by a octal digit (0-7) to avoid being interpreted as a different character, \x00 will be used to avoid ambiguity.

Parameters

ParameterTypeDescription
inputstringThe string to escape

Returns

string

Sting escaped for Javascript

Example

typescript
escapeJS('Hello\nWorld'); // "Hello\\nWorld"
escapeJS('"\\');          // "\\\"\\\\"
escapeJS('\b');           // "\\b"
escapeJS('\u20ac');       // "\\u20ac"