Class JavaScriptEscaper

java.lang.Object
org.cthing.escapers.JavaScriptEscaper

public final class JavaScriptEscaper extends Object
Escapes strings and character arrays according to the ECMA-262 ECMAScript® 2023 Language Specification. ECMAScript is typically referred to as JavaScript. Characters are escaped according to the following table:
Character Escaping
Character Escape Description
0x00 - 0x07 \uXXXX ASCII control characters
0x08 \b Backspace
0x09 \t Tab
0x0A \n Line feed
0x0B \uXXXX ASCII control character
0x0C \f Form feed
0x0D \r Carriage return
0x0E - 0x1F \uXXXX ASCII control characters
0x20 - 0x21 Unchanged Printable ASCII characters
0x22 \" Double quote
0x23 - 0x26 Unchanged Printable ASCII characters
0x27 \' Single quote
0x28 - 0x2E Unchanged Printable ASCII characters
0x2F \/ Forward slash
0x30 - 0x5B Unchanged Printable ASCII characters
0x5C \\ Backslash
0x5D - 0x7E Unchanged Printable ASCII characters
0x7F \uXXXX ASCII control character
0x80 - 0x10FFFF Unchanged by default or \uXXXX[\uXXXX] if JavaScriptEscaper.Option.ESCAPE_NON_ASCII specified ISO Latin-1, Unicode BMP and surrogate pairs
  • Method Details

    • escape

      public static String escape(CharSequence charSequence, JavaScriptEscaper.Option... options)
      Applies JavaScript escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(CharSequence charSequence, Set<JavaScriptEscaper.Option> options)
      Applies JavaScript escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, JavaScriptEscaper.Option... options) throws IOException
      Applies JavaScript escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException
      Applies JavaScript escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      public static String escape(char[] charArr, JavaScriptEscaper.Option... options)
      Applies JavaScript escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, int offset, int length, JavaScriptEscaper.Option... options)
      Applies JavaScript escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, Set<JavaScriptEscaper.Option> options)
      Applies JavaScript escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, int offset, int length, Set<JavaScriptEscaper.Option> options)
      Applies JavaScript escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, JavaScriptEscaper.Option... options) throws IOException
      Applies JavaScript escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, JavaScriptEscaper.Option... options) throws IOException
      Applies JavaScript escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException
      Applies JavaScript escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException
      Applies JavaScript escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null