The JavaScript one-liner is itself a regex: a character class of the five special characters and a replacer function. Try variations in the regex tester. For URLs rather than markup, use the URL encoder.
Questions
Which characters must be escaped in HTML?
In text content, & and <. Inside attribute values, & and the quote character that delimits the attribute. Escaping > and both quote types everywhere is the simple, safe habit — that is what the "Minimal" mode does.
Does escaping HTML prevent XSS?
It prevents it in HTML text and quoted attributes, which is where most templates put data. It does not make data safe inside <script> blocks, inline event handlers, style attributes or javascript: URLs — those need context-specific encoding. Frameworks like React, Vue and Jinja escape text by default.
Do I need entities for accented letters or emoji?
Not if your page is served as UTF-8 (with <meta charset="utf-8">), which nearly every modern page is. Numeric entities are only useful when a file must stay pure ASCII.
What is the difference between named and numeric entities?