JS Japanese full Half-width conversion

Source: Internet
Author: User

The customer's requirement is to enter half-width characters or Japanese kana, and the filtered results show all the results in half-width and full-width form that contain the character, and the same result is the input full-width. Here you need to convert all the characters you have entered into half-width and full-width, then match the results.
After searching the Internet, only the full-width conversion of the characters was found, and the Japanese almost didn't, and at last GitHub came to what they wanted.
GitHub Address: Https://github.com/shogo4405/KanaXS

The full half angle of the character is converted as follows: full-width turning half angle
function ToCDB(str) {       var tmp = "";       for (var i = 0; i < str.length; i++) {           if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {               tmp += String.fromCharCode(str.charCodeAt(i) - 65248);           }           else {               tmp += String.fromCharCode(str.charCodeAt(i));           }       }       return tmp}
Half angle turn full angle
function ToDBC(txtstring) {        var tmp = "";        for (var i = 0; i < txtstring.length; i++) {            if (txtstring.charCodeAt(i) == 32) {                tmp = tmp + String.fromCharCode(12288);            }            if (txtstring.charCodeAt(i) < 127) {                tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);            }        }    if(isEmpty(tmp)){        return txtstring;    }    return tmp;}

In the full-width turning half- angle method, the input full angle is converted to half-width, and the input half-angle is returned or half-width.
half-angle to full -width method, the input half-angle conversion to full-width, input full-width, return is empty, not conducive to the subsequent operation, so if the result is empty, then the input character is returned, the input full-width, return or full-width.

Japanese kana half angle turn full angle

On GitHub, the full-width method does not contain small characters, such as ぁ、ぃ、ぅ、ぇ、ぉ、っ、ゃ、ゅ、ょ , so I modified in the JS file, adding the Unicode code of these small characters in contrast. The complete code is as follows:

/** * Half angle のカタカナを full angle のカタカナに slew for します. * @example * "?????". Tozenkanacase (); アイウエオ* @return {String} */string.prototype.tozenkanacase = function () {var i, F, c, a = [], M = String.prototype.to    zenkanacase.mapping;        for (i = 0, F = this.length;i < F; i++) {c = this.charcodeat (i);    A.push (M[c] | | c);    }; return String.fromCharCode.apply (null, a);}; String.prototype.toZenkanaCase.MAPPING = {0xff67:0x30a1, 0xff68:0x30a3, 0XFF69:0X30A5, 0xff6a        : 0x30a7, 0xff6b:0x30a9, 0XFF70:0X30FC, 0XFF71:0X30A2, 0XFF72:0X30A4, 0XFF73:0X30A6, 0XFF74:0X30A8, 0XFF75:0X30AA, 0xff76:0x30ab, 0XFF77:0X30AD, 0XFF78:0X30AF, 0xff79:0        X30B1, 0xff7a:0x30b3, 0xff7b:0x30b5, 0xff7c:0x30b7, 0xff7d:0x30b9, 0XFF7E:0X30BB, 0XFF7F:0X30BD, 0XFF80:0X30BF, 0XFF81:0X30C1, 0xff82:0x30c4, 0xff83:0x30c6, 0xff84:0x3 0C8, 0xff85:0X30CA, 0XFF86:0X30CB, 0xff87:0x30cc, 0XFF88:0X30CD, 0XFF89:0X30CE, 0XFF8A:0X30CF, 0XFF8B:0X30D2, 0xff8c:0x30d5, 0xff8d:0x30d8, 0xff8e:0x30db, 0XFF8F:0X30DE, 0xff90:0x3        0DF, 0xff91:0x30e0, 0xff92:0x30e1, 0xff93:0x30e2, 0xff94:0x30e4, 0xff95:0x30e6, 0xff96:0x30e8, 0xff97:0x30e9, 0xff98:0x30ea, 0xff99:0x30eb, 0xff9a:0x30ec, 0xff9b:0x30e D, 0xff9c:0x30ef, 0xff9d:0x30f3, 0xff9e:0x309b, 0xff9f:0x309c, 0XFF66:0X30F2, 65 383:12,449,//ァ65384:12451,//ィ65385:12453,//ゥ65386:12455,//ェ65387:12457,//ォ6539 1:12,483,//ッ65388:12515,//ャ65389:12517,//ュ65390:12519//ョ};

JS Japanese full half-width conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.