Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 14, 2022 02:37 am GMT

Easily and safely escape HTML in one line

x => Object.assign(document.createElement('div'), { textContent: x }).innerHTML

True, you could also do it with

x => x.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;")

or similar, but if you do it the first way you don't have to remember the HTML entities


Original Link: https://dev.to/lionelrowe/easily-and-safely-escape-html-in-one-line-4lak

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To