es-toolkit/docs/reference/string/escape.md
raon0211 4fea8d2a00
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run
docs(escape, unescape): Add docs for escape & unescape
2024-09-03 23:53:24 +09:00

683 B

escape

Converts the characters "&", "<", ">", '"', and "'" in str to their corresponding HTML entities. For example, "<" becomes "&lt;".

Signature

function escape(str: string): string;

Parameters

  • str (string): The string to escape.

Returns

(string): The escaped string.

Examples

import { escape } from 'es-toolkit/string';

escape('This is a <div> element.'); // returns 'This is a &lt;div&gt; element.'
escape('This is a "quote"'); // returns 'This is a &quot;quote&quot;'
escape("This is a 'quote'"); // returns 'This is a &#39;quote&#39;'
escape('This is a & symbol'); // returns 'This is a &amp; symbol'