반응형
javascript 초성, 중성, 종성 분리
<script>
function han2jaso(str)
{
var cho = new Array (
0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139,
0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147,
0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e
);
var jung = new Array (
0x314f, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154,
0x3155, 0x3156, 0x3157, 0x3158, 0x3159, 0x315a,
0x315b,0x315c, 0x315d, 0x315e, 0x315f, 0x3160,
0x3161, 0x3162, 0x3163
);
var jong = new Array (
0x0000, 0x3131, 0x3132, 0x3133, 0x3134,0x3135,
0x3136, 0x3137, 0x3139, 0x313a, 0x313b, 0x313c,
0x313d, 0x313e, 0x313f, 0x3140, 0x3141, 0x3142,
0x3144, 0x3145, 0x3146, 0x3147, 0x3148, 0x314a,
0x314b, 0x314c, 0x314d, 0x314e
);
var chars = new Array();
var res = new Array();
var i1, i2, i3;
for (var i = 0; i < str.length; i++)
{
chars[i] = str.charCodeAt(i);
if (chars[i] >= 0xAC00 && chars[i] <= 0xD7A3)
{
i3 = chars[i] - 0xAC00;
i1 = i3 / (21 * 28);
i3 = i3 % (21 * 28);
i2 = i3 / 28;
i3 = i3 % 28;
res.push(String.fromCharCode(cho[parseInt(i1)]));
res.push(String.fromCharCode(jung[parseInt(i2)]));
if (i3 != 0x0000)
res.push(String.fromCharCode(jong[parseInt(i3)]));
}
else {
res.push(String.fromCharCode(chars[i]));
}
}
return res;
}
alert(han2jaso("안녕하세요"));
</script>
반응형