JavaScript escapeHtml()

Jmnote (토론 | 기여)님의 2017년 6월 18일 (일) 00:21 판 (새 문서: ==개요== ;JavaScript escapeHtml() <source lang='JavaScript'> function escapeHtml(str) { var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": '''...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

JavaScript escapeHtml()
function escapeHtml(str) {
	var map = {
		'&': '&amp;',
		'<': '&lt;',
		'>': '&gt;',
		'"': '&quot;',
		"'": '&#039;'
	};
	return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}
console.log( escapeHtml("<a href='test'>Test</a>") );
// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
console.log( escapeHtml("This is some <b>bold</b> text.") );
// This is some &lt;b&gt;bold&lt;/b&gt; text.

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}