본문 바로가기
개발정리 (nodeJS)

[nodeJS] html-escaper 모듈을 이용하여 HTML 이스케이프 문자 파싱하기

by 할리갈리0 2023. 11. 11.

HTML 이스케이프는 HTML 코드에서 특수 문자를 안전하게 처리하거나 보여주기 위해 필요합니다. 

웹 페이지의 구조를 파괴할 수 있는 특수 문자를 방지하거나, 사용자가 입력한 데이터를 안전하게 표시할 수 있습니다. 

 

1. html-escaper 모듈 설치

Node.js에서 HTML 이스케이프를 쉽게 다룰 수 있게 해주는 'html-escaper' 모듈 설치

npm install html-escaper

 

 

2. html-escaper 모듈 불러오기

 import 구문을 이용해 'html-escaper' 모듈 불러오기

import {escape, unescape} from 'html-escaper';

 

 

3. 이스케이프 처리하기

'html-escaper' 모듈의 'escape' 함수를 이용하여 HTML 문자열에서 특수 문자를 이스케이프 문자로 바꾸기

let str = "<div>Hello World!</div>";
let escapedStr = escape(str);
console.log(escapedStr); // &lt;div&gt;Hello World!&lt;/div&gt;

 

 

4. 이스케이프 해제하기

반대로 'unescape' 함수를 이용하면 이스케이프 문자를 원래의 특수 문자로 바꾸기 가능

let str = "&lt;div&gt;Hello World!&lt;/div&gt;";
let unescapedStr = unescape(str);
console.log(unescapedStr); // <div>Hello World!</div>

 

 

HTML 이스케이프는 웹 페이지의 보안과 데이터 표현의 정확성을 위해 중요한 역할을 하니 'html-escaper' 모듈을 활용하면 좋을 거 같습니다.

 

[참고 페이지]

https://www.npmjs.com/package/html-escaper

반응형