escape function

String escape(
  1. String text
)

Escapes special characters in the given text to their corresponding HTML entities.

This function replaces the characters &, ", <, and > with their corresponding HTML entities.

text: The text to escape.

Implementation

String escape(String text) {
  return text
      .replaceAll('&', '&amp;')
      .replaceAll('"', '&quot;')
      .replaceAll('<', '&lt;')
      .replaceAll('>', '&gt;');
}