2010년 11월 6일 토요일

자바스크립트 특수문자 처리(따옴표 등등...)

특수 문자
Javascript는 직접 입력할 수 없는 일부 문자를 문자열에 포함시킬 수 있는 특수 문자를 제공한다. 각 특수 문자는 역슬래시로 시작한다. 역슬래시는 다음 문자가 특수 문자임을 Javascript 해석기에 알려주는 데 사용되는 이스케이프 문자이다.  document.write('escape("\\b")=',escape('\b')); escape("\b")=%08
이스케이프 시퀀스 문자 ASCII 영문
\b 백스페이스 8 Backspace
\t 수평 탭(Ctrl-I) 9 Tab
\n 줄 바꿈 10 Newline, Line feed
\f 용지 공급 12 Form feed
\r 캐리지 리턴 13 Carriage return
\" 이중따옴표 34 Duble quotation
\' 단일따옴표 39 Single quotation
\\ 역슬래시 92 Back slash
역슬래시 자체는 이스케이프 문자로 사용되므로 스크립트에서 직접 입력할 수 없다.
역슬래시를 사용하려면 역슬래시를 두번 입력해야 한다(\\).


 escape (jsString) 
누구나 ASCII 글자로 접속할 수 있도록 엔코딩(encoding)한다.
인수
escape : (엔코딩 기능함수)
필수적인 인수로 최상위 내장 기능함수이다.
jsString : (엔코딩할 문자열)
엔코딩할 문자열의 개체이거나 문자열이다.

설명
unescape() 기능함수와 같이 보면 쉽게 이해될 것이다.
escape() 기능함수는 문자열을 네트워크를 통하여 누구나 ASCII 글자로 접속할 수 있도록 엔코딩한다.
아래 ASCII 69의 글자는 escape()하나 unescape()하나 아무 변화가 없으나, 그 이외의 글자는 모두 엔코딩하여야 다른 사용자가 널리 사용할 수 있기 때문이다.
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
1234567890
@*-_+./
모든 다른 문자들은 escape() 기능함수로 엔코딩 표준에 따라서 16진수 2 디짓(%xx)이나 4 디짓(%uxxxx) 데이터로 전환하여야 한다. 예를 들어 빈칸(' ')은 %20, 세미콜론(;)은 %3B, '한글'은 '  document.write(escape('한글')) %uD55C%uAE00'이 된다.
escape() 기능함수로 엔코딩 된 데이터는 unescape() 기능함수로 원래 상태로 디코딩하게 된다.
특기
escape()unescape() 기능함수는 자체가 메서드 역할을 하므로 어떤 object에서도 method와 연관되지 않는다.
문자열등 String 개체에서는 이들 기능함수들은 전역 기능성 메서드로 사용된다.
escape()unescape() 메서드는 URI(Uniform Resource Identifiers)를 인코딩하는 데는 사용할 수 없으므로, 대신 encodeURI, decodeURIencodeURIComponent, decodeURIComponent 메서드를 사용한다.



expTdArray=new Array(); expTdArray.push('../refers/msversion.html','JScript 버전 1
'); expTdArray.push('../refers/msversion.html','Javascript 버전 1.0'); expTable('ver',expTdArray) expTdArray=new Array(); expTdArray.push('../object/Global.html','Global 개체'); expTable('apply',expTdArray) expTdArray=new Array(); expTdArray.push('../function/escape.html','escape() 기능함수'); expTdArray.push('../method/encodeURI.html','encodeURI 메서드'); expTdArray.push('../method/encodeURIComponent.html','encodeURIComponent 메서드'); expTdArray.push('../function/unescape.html','unescape() 기능함수'); expTdArray.push('../method/decodeURI.html','decodeURI 메서드'); expTdArray.push('../method/decodeURIComponent.html','decodeURIComponent 메서드'); expTdArray.push('../object/String.html','String 개체'); expTdArray.push('http://trio.co.kr/webrefer/ascii.html','ASCII 조견표'); expTdArray.push('http://trio.co.kr/webrefer/html/sgml/entities.html','HTML 4의 글자 엔티티(entity)'); expTable('refer',expTdArray);

expTdArray=new Array(); expTdArray.push('../refers/specialchar.html','
특수문자'); expTdArray.push('../function/escape.html','escape() 기능함수'); expTdArray.push('../refers/reguSyntax.html','정규식 특수문자'); expTdArray.push('http://trio.co.kr/webrefer/ascii.html','ASCII 조견표'); expTdArray.push('http://trio.co.kr/webrefer/html/sgml/entities.html','HTML 4의 글자 엔티티(entity)'); expTable('refer',expTdArray);


http://www.dude.co.kr




P 이경철님의 파란블로그에서 발행된 글입니다.

댓글 없음:

댓글 쓰기