December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 8 | Converting HTML Markup to HTML Entities with replaceAll() |
Imagine you want to publish programming tutorials on your website that contain HTML markup. But you encounter an issue where web browsers interpret the HTML tags in your examples instead of displaying them. This happens because browsers assume that you use the tags to structure your web page.
You need to represent the markup in a way that prevents the browser from interpreting it.
Use the replaceAll() method to replace the components of HTML tags with entities:
| | function escapeHTML(str) { |
| | return str |
| | .replaceAll('&', '&') |
| | .replaceAll('<', '<') |
| | .replaceAll('>', '>') |
| | .replaceAll( |
Read now
Unlock full access