sitelink1 https://blog.naver.com/bb_/221449936369 
sitelink2  
sitelink3  
sitelink4  
extra_vars4  
extra_vars5  
extra_vars6  

charcode 32는 공백(Space)이고, 160은 NBSP(non-breaking space) 이다.

두 가지 모두 공백이지만, 같은 소스코드의 페이지라도 브라우저에 따라 컴포넌트 내의 문자열을 서로 다르게 가져온다. (ex: textarea 에서 문자열을 가져올 때)

익스플로러는 32로만 가져오는데, 크롬은 32와 160을 섞어서 가져오는 경우가 많은듯 하다.

 

따라서 두 가지 공백을 하나로 통일해주는 코드가 필요하다.

구글링 결과 크게 다음 3가지 코드가 있다.

(1) 32와 160을 모두 공백(32)으로 변경
return _text.replace(/\s+/g, " ");
    
(2) 160만 공백(32)으로 변경
return _text.replace(new RegExp(String.fromCharCode(160),"g"), " ");
    
(3) 160만 공백(32)으로 변경
return _text.replace(/\xA0/g, " ");

 

다음은 직접 작성한 예제소스.

로컬에 html 파일로 저장해서 실행하면 된다.

 

<html>
<head>
<script>
window.onload = function() {
    doTest();
}

 

function doTest() {
    alert("doTest");
    
    var beforeText = "대" + String.fromCharCode(32) + "학" + String.fromCharCode(160) + "교";
    var afterText = replaceNbsp(beforeText);
    
    alert("BEFORE : " + getCharCodeFromText(beforeText));
    alert("AFTER : " + getCharCodeFromText(afterText));
}

 

// 문자열을 캐릭터코드로 변경해서 리턴
function getCharCodeFromText(_text) {
    if (_text == null || _text == "") {
        return "empty";
    }

    var result = "";
    var len = _text.length;
    for (var i=0; i<len; i++) {
        if (result.length > 0) {
            result += "/";
        }
        result += _text.charCodeAt(i);
    }
    
    return result;
}

 

function replaceNbsp(_text) {
    if (_text == null || _text == "") {
        return "";
    }

    // (1) 32와 160을 모두 공백(32)으로 변경
    // return _text.replace(/\s+/g, " ");
    
    // (2) 160만 공백(32)으로 변경
    return _text.replace(new RegExp(String.fromCharCode(160),"g"), " ");
    
    // (3) 160만 공백(32)으로 변경
    // return _text.replace(/\xA0/g, " ");
}
</script>
</head>
<body>
</body>
</html>

 

번호 제목 글쓴이 날짜 조회 수
197 XMLHttpRequest.timeout 황제낙엽 2018.11.03 248
196 IFrames and cross-domain security file 황제낙엽 2012.01.13 246
195 UTF-8 한글 초성 추출 (자바스크립트) 황제낙엽 2019.05.07 243
194 Why does this simple Javascript prototype not work in IE? 황제낙엽 2011.03.24 242
193 Function.apply and Function.call in JavaScript 황제낙엽 2011.10.07 238
192 브라우저에서 뒤로 가기 막기와 펑션키(function key) 막기 황제낙엽 2005.10.21 236
191 두 서버의 자원을 접근하는 클라이언트 프레임웍(Next.js)에서의 CORS오류 file 황제낙엽 2021.12.05 231
190 JavaScript Closures for Dummies 황제낙엽 2009.04.08 227
189 Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
188 javascript array contains method 황제낙엽 2011.08.19 210
187 char to hex string 황제낙엽 2011.11.29 206
186 How to use Rhino to script Java classes. 황제낙엽 2008.07.14 198
185 Page Refresh/Reload 황제낙엽 2007.08.24 197
184 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 194
183 Reference Count (순환참조) 황제낙엽 2011.11.24 191
182 JavaScript Form Validation file 황제낙엽 2008.11.24 186
181 code compressor & decompressor 황제낙엽 2015.01.02 181
180 자바스크립트의 데이터 타입과 변수 황제낙엽 2008.08.06 179
179 SelectBox 밑에 CheckBox가 포함된 리스트 만들기 file 황제낙엽 2007.01.16 178
178 서비스 워커 file 황제낙엽 2020.05.25 177