Escape HTML JavaScript

Funkcja do escapowania textu html.

function escapeHtml(text) {
	 const map = {
		  '&': '&',
		  '<': '<',
		  '>': '>',
		  '"': '"',
		  "'": '''
	 };

	 if (typeof text !== 'string') {
		  return text.toString().replace(/[&<>"']/g, function(m) {
				return map[m];
		  });
	 }

	 return text.replace(/[&<>"']/g, function(m) {
		  return map[m];
	 });
}

Dodaj komentarz

This site uses Akismet to reduce spam. Learn how your comment data is processed.