Javascript 일본어 전각 반각 - 길이 판단
=> 아래의 내용을 그대로 사용하지는 못하고, 업무에 따라서, 커스터마이징 해서 사용하면 될 것 같다.
Javascript judges the length of Japanese full-width half-width
Today I encountered the need to determine the number of full-width and half-width text input in the input box. For example, you can enter 10 full-width characters and 20 half-width characters. After searching on the Internet, there is an article about JavaScript , the focus is on converting strings into unicode encoding, and AS and Javascript have ready-made charCodeAt () function.
Not all Unicode between 0-255 is 1 byte long! ! Moreover, the Japanese Kana has a half-width form (Unicode is 65377-65439, except 65381)! ! , Not only these are half-width, there are many characters also half-width! ! Therefore, it is impossible to judge whether it is in 0-255.
Considering that Unicode includes all kinds of characters from all countries, and these characters are full-width and half-width mixed, so there will not be a perfect JS method to check half-width characters. But because other characters are generally not used, most of the future work is on Japanese projects, so I still use the method of 0-255, then cut out some full-width, plus the verification of half-width Japanese, and write the following method For research use:
function calcUTFByte (str)
{
var len = 0;
for (var i = 0; i <str.length; i ++) {
var temp = str.charCodeAt (i);
if (temp> = 0 && temp <= 254) {
//The following characters are full-width within 0-255
if (temp == 162
|| temp == 163
|| temp == 167
|| temp == 168
|| temp == 171
|| temp == 172
|| temp == 175
|| temp == 176
|| temp == 177
|| temp == 180
|| temp == 181
|| temp == 182
|| temp == 183
|| temp == 184
|| temp == 187
|| temp == 215
|| temp == 247) {
len + = 2;
}
len ++;
} else if (temp> = 65377 && temp <= 65439) {
if (temp == 65381) {
len + = 2;
}
len ++;
} else {
len + = 2;
}
}//for end
return len;
}
출처: https://titanwolf.org/Network/Articles/Article?AID=da79219f-f754-434b-acce-72ba7b862b44#gsc.tab=0
[그 외]
// 전각이면 true, 반각이면 false
function checkIsZenkaku(value) {
for (var i = 0; i < value.length; ++i) {
var c = value.charCodeAt(i);
if (c < 256 || (c >= 0xff61 && c <= 0xff9f)) {
return false;
}
}
return true;
}
댓글 영역