이스케이프 시퀀스 (Escape Sequence) 란?
- 과거에 프린트 할 수 없는 특수한 문자를 표현하려고 사용하기 시작됐다.
- 초기 ASCII 코드에는 글자나 숫자같은 프린트 가능한 문자만 다뤘다.
- 종이에 쓸 수는 없어도 분명 개념상 있는 문자를 다룬다.
- 이를테면 개행 (new line), 탭 (tab), 캐리지 리턴 등이 있다.
- 프로그래밍에서 문자열을 취급할 때 역 슬래시(
\)를 이스케이프 캐릭터로 사용한다.
- 이스케이프 캐릭터는 이스케이프 시퀀스의 시작을 알리는 용도이다.
자바스크립트 예제
\n: 개행
\t: 탭
\b: 백스페이스
\r: 캐리지 리턴
\': 작은 따옴표
\": 큰 따옴표
console.log(String.raw`원본: a\nb`);
console.log("a\nb");
console.log("----");
console.log(String.raw`원본: a\tb`);
console.log("a\tb");
console.log("----");
console.log(String.raw`원본: a\bb`);
console.log("a\bb");
console.log("----");
console.log(String.raw`원본: a\rb`);
console.log("a\rb");
/*
출력 결과:
원본: a\nb
a
b
----
원본: a\tb
a b
----
원본: a\bb
ab
----
원본: a\rb
a
b
*/