Function htmlEscape
Source: Html.ts
Escapes HTML special characters in a string, replacing them with their HTML entity equivalents. Optionally, additional custom character replacements can be applied after the built-in escaping.
Built-in replacements: & → &, < → <, > → >, " → ", ' → '.
Import
ts
import { htmlEscape } from '@litert/utils-string';Signature
ts
function htmlEscape(
text: string,
extraReplacement?: ReadonlyArray<readonly [string, string]>,
): string;Parameters
Parameter
text: stringThe input string to escape.
Parameter
extraReplacement?: ReadonlyArray<readonly [string, string]>Optional list of additional
[from, to]replacement pairs applied after the built-in escaping. Each pair replaces all occurrences of thefromstring with thetostring.
Return Value
The escaped string.
Examples
ts
import { htmlEscape } from '@litert/utils-string';
htmlEscape('<script>alert("XSS")</script>');
// '<script>alert("XSS")</script>'
htmlEscape("It's a <b>test</b>");
// 'It's a <b>test</b>'
// Custom extra replacements
htmlEscape('Hello World', [[' ', ' ']]);
// 'Hello World'