@metreeca/core - v0.9.22
    Preparing search index...

    Function escape

    • 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 / \UXXXXXXXX convention 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: pattern widens or narrows the selection, for instance to escape every non-ASCII character or the characters that syntax reserves, and escapes supplies the short forms it defines, as a Resolver listing them or computing them on demand. Selected supplementary code points draw on the eight-digit \UXXXXXXXX form.

      Irregular matches

      pattern is 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:

      • escapes is consulted with the whole match, so a short form may be assigned to a multi-character match and is emitted as it stands
      • failing that, the match is spelled from its first code point alone and the rest of it is dropped, as a numeric escape denotes a single code point and nothing in the syntax carries the remainder
      • a zero-width match, which selects no character at all, is spelled as the escape of U+FFFD REPLACEMENT CHARACTER, the stand-in toWellFormed likewise substitutes for text that denotes no character

      Parameters

      • string: string

        The string to escape

      • pattern: RegExp = ...

        The pattern selecting the characters to escape; must be global and is expected to match single characters; defaults to the JSON set

      • escapes: Resolver = JSONEscapes

        The lookup supplying the escapes selected characters take in preference to the numeric form; defaults to the two-character JSON escapes

      Returns string

      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

      If pattern doesn't carry the g flag