码迷,mamicode.com
首页 > 其他好文 > 详细

Kazakh Latin-Arabic-Cyrillic Converter

时间:2018-06-22 01:12:49      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:document   replace   sele   tle   display   width   regexp   convert   sel   

技术分享图片
<!DOCTYPE html>
<html>
    <head>

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />

        <meta charset="utf-8" />

        <title>NoorTy - Converter</title>

        <script>
function NoorTyConverter() {
    var toeteJazuwlar = "([\u060c\u061f\u061b\u0621\u0627\u0628\u062a\u062c\u062d\u062f\u0631-\u0634\u0639\u0641-\u0646\u0648-\u064a\u067e\u0686\u06ad\u06af\u06be\u06c6\u06c7\u06cb\u06d5]+)";
    this.cyrillic2arabic = function(a) {
        a = almastiruw(a, ["А", "?", "Б", "В", "Г", "?", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "?", "Л", "М", "Н", "?", "О", "?", "П", "Р", "С", "Т", "У", "?", "?", "Ф", "Х", "?", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "?", "Ь", "Э", "Ю", "Я", "\\,", "\\;", "\\?"], ["?", "??", "?", "?", "?", "?", "?", "?", "??", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "??", "?", "?", "?", "?", "?", "?", "??", "?", "?", "?", "?", "?", "?", "??", "", "?", "??", "", "?", "??", "??", "?", "?", "?"], "gi");
        a = a.replace(/???/g, "??");
        return a.replace(new RegExp(toeteJazuwlar, "g"), function(a) {
            function b(c) {
                return c.replace(/\u0621/g, "");
            };
            return (0 <= a.search(/\u0621/) && 0 > a.search(/[\u06af\u0643\u06d5]/)) ? "?" + b(a) : b(a);
        });
    }
    this.arabic2latin = function(a) {
        a = a.replace(new RegExp(toeteJazuwlar, "g"), function(a) {
            var b, c;
            b = ["?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"];
            c = ["a", "b", "v", "g", "g‘", "d", "e", "j", "z", "i‘", "k", "q", "l", "m", "n", "n‘", "o", "p", "r", "s", "t", "y‘", "u", "f", "h", "h", "c‘", "s‘", "y", ",", ";", "?"];
            if (0 > a.search(/[\u0621\u06d5\u06af\u0643]/)) {
                a = almastiruw(a, b, c, "g");
            } else {
                a = almastiruw(a, b, c, "g");
                a = a.replace(/([aou])/g, "$1‘");
                a = a.replace(/y(?!\‘)/g, "i");
                a = a.replace(/\u0621/g, "");
            };
            return tuezetuew(a);
        });
        return basAerip(a);
    }
    this.latin2cyrillic = function(a) {
        a = a.replace(/[\u2019\u2018\u0060]/g, "‘");
        a = a.replace(/([bdf-hj-np-tvz]|[cgnsy]‘)(i‘)(a)/gi, "$1$2$2$3");
        a = almastiruw(a, 
            ["A‘", "a‘", "C‘", "c‘", "G‘", "g‘", "I‘", "i‘", "N‘", "n‘", "O‘", "o‘", "S‘", "s‘", "U‘", "u‘", "Y‘", "y‘", "A", "a", "B", "b", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "Y", "y", "Z", "z"], 
            ["?", "?", "Ч", "ч", "?", "?", "И", "и", "?", "?", "?", "?", "Ш", "ш", "?", "?", "У", "у", "А", "а", "Б", "б", "Д", "д", "Е", "е", "Ф", "ф", "Г", "г", "Х", "х", "?", "?", "Ж", "ж", "К", "к", "Л", "л", "М", "м", "Н", "н", "О", "о", "П", "п", "?", "?", "Р", "р", "С", "с", "Т", "т", "?", "?", "В", "в", "Ы", "ы", "З", "з"]
        );
        a = a.replace(/([Аа??Оо??????Ыы??Ее])и/g, "$1й");
        a = a.replace(/([Аа??Оо??????Ыы??Ее])И/g, "$1Й");
        a = a.replace(/[ий][Аа??]/g, "я");
        a = a.replace(/[ИЙ][Аа??]/g, "Я");
        a = a.replace(/[ий][Уу]/g, "ю");
        a = a.replace(/[ИЙ][Уу]/g, "Ю");
        a = a.replace(/ш[Шш]/g, "щ");
        a = a.replace(/Ш[Шш]/g, "Щ");
        return a;
    }
    function basAerip(a) {
        a = a.toLowerCase();
        a = a.replace(/([\n\u0021\u002e\u003f]+|^\s*)([\s\u0021-\u0040\u005b-\u0060\u007b-\u007e\u00a1-\u00bf\u00d7\u00f7]*)./g, function(a) {
            return a.toUpperCase();
        });
        return a;
    }
    function almastiruw(a, b, c, d) {
        d = d || "g";
        for (var e in b) {
            a = a.replace(new RegExp(b[e], d), c[e]);
        }
        return a;
    }
    function tuezetuew(a) {
        var b, c;
        b = ["c‘", "f", "v", "tele", "i‘ka", "i‘o‘n", "ju‘n‘go", "[ln]i‘[sz]"];
        c = "([a-dfh-jl-z‘]*|g‘*)";
        a = a.replace(
            new RegExp("g‘" + c + "|[qh]" + c + "|(a‘.a‘)" + c + "|([er])" + c + "([ao])" + c + "|([a-z‘]*)(" + b.join("|") + ")([a-z‘]*)", "gi"),
            function(a) {
                return a.replace(/([aou])\‘/gi, "$1");
            }
        );
        a = a.replace(
            new RegExp("g‘" + c + "|[qh]" + c + "|[aou](?!‘)" + c, "gi"),
            function(a) {
                return a.replace(/i(?!\‘)/gi, "y");
            }
        );
        return almastiruw(a, 
            ["ko‘lba‘i‘", "a‘y‘zimen", "ba‘simen"], 
            ["ko‘lbai‘", "ay‘zymen", "basymen"], 
            "gi"
        );
    }
}
        </script>
        <style>
textarea {
    display: block;
    width: 99%;
    height: 20em;
}
        </style>
    </head>

    <body> 
        <h1>NoorTy - Converter</h1>
        <textarea></textarea>
        <select>
            <option value="Latin2Cyrillic">Latin => Cyrillic</option>
            <option value="Cyrillic2Arabic">Cyrillic => Arabic</option>
            <option value="Arabic2Latin">Cyrillic => Arabic</option>
        </select>
        <button>Convert</button>
        <script>
var Converter = new NoorTyConverter();
document.getElementsByTagName(‘button‘)[0].onclick = function() {document.getElementsByTagName(‘textarea‘)[0].value = (document.getElementsByTagName("select")[0].options[document.getElementsByTagName("select")[0].selectedIndex].value == "Cyrillic2Arabic") ? Converter.cyrillic2arabic(document.getElementsByTagName(‘textarea‘)[0].value) : ((document.getElementsByTagName("select")[0].options[document.getElementsByTagName("select")[0].selectedIndex].value == "Arabic2Latin") ? Converter.arabic2latin(document.getElementsByTagName(‘textarea‘)[0].value) : Converter.latin2cyrillic(document.getElementsByTagName(‘textarea‘)[0].value))}
        </script>
    </body> 
</html> 
View Code

Kazakh Latin-Arabic-Cyrillic Converter

标签:document   replace   sele   tle   display   width   regexp   convert   sel   

原文地址:https://www.cnblogs.com/nurtay/p/converter.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!