The string to escape
The pattern selecting the characters to escape; must be global and is expected to match single characters; defaults to the JSON set
The lookup supplying the escapes selected characters take in preference to the numeric form; defaults to the two-character JSON escapes
A copy of string with every character pattern selects replaced by the escape escapes assigns it, or
by its numeric escape where escapes assigns none, a match spanning several characters contributing the escape
of its first code point alone
Escapes a string as string literal content.
Rewrites every selected character as the escape assigned to it, or, failing that, with the upper-case
\uXXXX/\UXXXXXXXXconvention shared by many plain-text syntaxes; everything else is left as it is.JSON escaping —
escape(string)Escapes exactly what a JSON string may not carry as it is: the quotation mark, the reverse solidus and the C0 control characters, seven of which take a two-character form (
\",\\,\b,\f,\n,\r,\t), plus isolated surrogates, which denote no character. Non-ASCII text and paired surrogates are left as they are, as JSON admits them; escaping isolated surrogates leaves the result well-formed UTF-16 text, so it survives encoding to UTF-8 and reads back through a JSON parser as the string it was built from, ill-formed halves included.Custom escaping —
escape(string, pattern, escapes?)Targets a syntax of the caller's choosing:
patternwidens or narrows the selection, for instance to escape every non-ASCII character or the characters that syntax reserves, andescapessupplies the short forms it defines, as a Resolver listing them or computing them on demand. Selected supplementary code points draw on the eight-digit\UXXXXXXXXform.Irregular matches
patternis expected to select one character at a time; a match spanning more or less than one is still handled, rather than rejected, but only on the terms the numeric form allows:escapesis consulted with the whole match, so a short form may be assigned to a multi-character match and is emitted as it standsU+FFFDREPLACEMENT CHARACTER, the stand-in toWellFormed likewise substitutes for text that denotes no character