揮発性のメモ2

http://d.hatena.ne.jp/iww/

OCRで読み込んだ電話番号とかを綺麗に整形する 計算器




function calc_teltel(){
    var text = $("#tel_text").val();

    text = fascii2ascii(text); // 全角半角変換
    text = text.replace(/\s+/g, ""); // 空白除去
    text = text.replace(/(\w)(ー+)/g, "$1-"); // 英数字の後ろの長音をハイフンに
    text = text.replace(/[()−-]+/g, "-"); // かっこはハイフンに
    text = text.replace(/^-/g, ""); // 先頭のハイフンは除去

    if(text.indexOf("@")>0) text = text.toLowerCase(); // メールアドレスっぽかったら小文字化

    $("#tel_text").val(text);
    return false;
}

// http://blog.livedoor.jp/dankogai/archives/50984862.html
var fascii2ascii = (function(){
  var cclass
   = '['+String.fromCharCode(0xff01)+'-'+String.fromCharCode(0xff5e)+']';
  var re_fullwidth = new RegExp(cclass, 'g');
  var substitution = function(m){
    return String.fromCharCode(m.charCodeAt(0) - 0xfee0); // 0xff00 - 0x20
  };
  return function(s){ return s.replace(re_fullwidth, substitution) };
})();