참고 : http://d2.naver.com/helloworld/76650
String.prototype.normalizeEx = function () {
if(typeof this.normalize == 'function') {
return this.normalize('NFC');
} else {
return this.normalizeNFCForIE();
}
}
String.prototype.normalizeNFCForIE = function() {
var result = '';
var i, c;
var cho = -1, jung = -1, jong = -1;
for (i = 0 ; i < this.length ; ++i) {
c = this.charCodeAt(i);
if (0x1100 <= c && c < 0x1161) {
// 새 초성이 나오면 기존 초, 중, 종을 조합
if(cho >= 0) {
result += combineKorean(cho, jung, jong);
cho = jung = jong = -1;
}
cho = c - 0x1100;
} else if (0x1161 <= c && c < 0x11A8) {
jung = c - 0x1161;
} else if (0x11A8 <= c && c <= 0x11FF) {
jong = c - 0x11A8;
} else {
// 한글이 아니면 기존 초, 중, 종을 조합
if(cho >= 0 || jung >= 0 || jong >= 0) {
result += combineKorean(cho, jung, jong);
cho = jung = jong = -1;
}
result += String.fromCharCode(c);
}
}
// 루프가 끝난 후 아직 한글이 남아있으면 초, 중, 종을 조합
if(cho >= 0 || jung >= 0 || jong >= 0) {
result += combineKorean(cho, jung, jong);
}
return result.isNullOrEmpty()? this : result;
}
combineKorean = function (cho, jung, jong) {
var jongsung = (jong == 0 ? 0 : jong + 1);
var code = 0xAC00 + ((cho * 21) + jung) * 28 + jongsung;
return String.fromCharCode(code);
}
String.prototype.isNullOrEmpty = function() {
return (this.constructor !== String || this.trimAll() === '');
};