標準変換

提供: MeryWiki
移動先: 案内検索

半角カタカナを全角カタカナに、全角英数字を半角英数字に変換します。文字列変換のサンプルとしても最適かも。

// -----------------------------------------------------------------------------
// 標準変換
//
// Copyright (c) Kuro. All Rights Reserved.
// www:    http://www.haijin-boys.com/
// -----------------------------------------------------------------------------
 
if (document.selection.Text == "")
  document.selection.SelectAll();
var map = new Array();
map[" "] = " ";
map["!"] = "!";
map["“"] = "\"";
map["”"] = "\"";
map["#"] = "#";
map["$"] = "$";
map["%"] = "%";
map["&"] = "&";
map["’"] = "'";
map["("] = "(";
map[")"] = ")";
map["*"] = "*";
map["+"] = "+";
map[","] = ",";
map["-"] = "-";
map["."] = ".";
map["/"] = "/";
map["0"] = "0";
map["1"] = "1";
map["2"] = "2";
map["3"] = "3";
map["4"] = "4";
map["5"] = "5";
map["6"] = "6";
map["7"] = "7";
map["8"] = "8";
map["9"] = "9";
map[":"] = ":";
map[";"] = ";";
map["<"] = "<";
map["="] = "=";
map[">"] = ">";
map["?"] = "?";
map["@"] = "@";
map["A"] = "A";
map["B"] = "B";
map["C"] = "C";
map["D"] = "D";
map["E"] = "E";
map["F"] = "F";
map["G"] = "G";
map["H"] = "H";
map["I"] = "I";
map["J"] = "J";
map["K"] = "K";
map["L"] = "L";
map["M"] = "M";
map["N"] = "N";
map["O"] = "O";
map["P"] = "P";
map["Q"] = "Q";
map["R"] = "R";
map["S"] = "S";
map["T"] = "T";
map["U"] = "U";
map["V"] = "V";
map["W"] = "W";
map["X"] = "X";
map["Y"] = "Y";
map["Z"] = "Z";
map["["] = "[";
map["¥"] = "\\";
map["]"] = "]";
map["^"] = "^";
map["_"] = "_";
map["‘"] = "`";
map["’"] = "`";
map["a"] = "a";
map["b"] = "b";
map["c"] = "c";
map["d"] = "d";
map["e"] = "e";
map["f"] = "f";
map["g"] = "g";
map["h"] = "h";
map["i"] = "i";
map["j"] = "j";
map["k"] = "k";
map["l"] = "l";
map["m"] = "m";
map["n"] = "n";
map["o"] = "o";
map["p"] = "p";
map["q"] = "q";
map["r"] = "r";
map["s"] = "s";
map["t"] = "t";
map["u"] = "u";
map["v"] = "v";
map["w"] = "w";
map["x"] = "x";
map["y"] = "y";
map["z"] = "z";
map["{"] = "{";
map["|"] = "|";
map["}"] = "}";
map["~"] = "~";
map["。"] = "。";
map["「"] = "「";
map["」"] = "」";
map["、"] = "、";
map["・"] = "・";
map["ヲ"] = "ヲ";
map["ァ"] = "ァ";
map["ィ"] = "ィ";
map["ゥ"] = "ゥ";
map["ェ"] = "ェ";
map["ォ"] = "ォ";
map["ャ"] = "ャ";
map["ュ"] = "ュ";
map["ョ"] = "ョ";
map["ッ"] = "ッ";
map["ー"] = "ー";
map["ア"] = "ア";
map["イ"] = "イ";
map["ウ"] = "ウ";
map["エ"] = "エ";
map["オ"] = "オ";
map["カ"] = "カ";
map["キ"] = "キ";
map["ク"] = "ク";
map["ケ"] = "ケ";
map["コ"] = "コ";
map["ガ"] = "ガ";
map["ギ"] = "ギ";
map["グ"] = "グ";
map["ゲ"] = "ゲ";
map["ゴ"] = "ゴ";
map["サ"] = "サ";
map["シ"] = "シ";
map["ス"] = "ス";
map["セ"] = "セ";
map["ソ"] = "ソ";
map["ザ"] = "ザ";
map["ジ"] = "ジ";
map["ズ"] = "ズ";
map["ゼ"] = "ゼ";
map["ゾ"] = "ゾ";
map["タ"] = "タ";
map["チ"] = "チ";
map["ツ"] = "ツ";
map["テ"] = "テ";
map["ト"] = "ト";
map["ダ"] = "ダ";
map["ヂ"] = "ヂ";
map["ヅ"] = "ヅ";
map["デ"] = "デ";
map["ド"] = "ド";
map["ナ"] = "ナ";
map["ニ"] = "ニ";
map["ヌ"] = "ヌ";
map["ネ"] = "ネ";
map["ノ"] = "ノ";
map["ハ"] = "ハ";
map["ヒ"] = "ヒ";
map["フ"] = "フ";
map["ヘ"] = "ヘ";
map["ホ"] = "ホ";
map["バ"] = "バ";
map["ビ"] = "ビ";
map["ブ"] = "ブ";
map["ベ"] = "ベ";
map["ボ"] = "ボ";
map["パ"] = "パ";
map["ピ"] = "ピ";
map["プ"] = "プ";
map["ペ"] = "ペ";
map["ポ"] = "ポ";
map["マ"] = "マ";
map["ミ"] = "ミ";
map["ム"] = "ム";
map["メ"] = "メ";
map["モ"] = "モ";
map["ヤ"] = "ヤ";
map["ユ"] = "ユ";
map["ヨ"] = "ヨ";
map["ラ"] = "ラ";
map["リ"] = "リ";
map["ル"] = "ル";
map["レ"] = "レ";
map["ロ"] = "ロ";
map["ワ"] = "ワ";
map["ン"] = "ン";
map["ヴ"] = "ヴ";
map["゙"] = "゛";
map["゚"] = "゜";
var s1 = document.selection.Text;
var s2 = "";
for (var i = 0; i < s1.length; i++) {
  if (i + 1 < s1.length) {
    var c = map[s1.substring(i, i + 2)];
    if (c) {
      s2 += c;
      i++;
      continue;
    }
  }
  var c = map[s1.substring(i, i + 1)];
  if (c) {
    s2 += c;
    continue;
  }
  s2 += s1.substring(i, i + 1);
}
document.selection.Text = s2;