全角<-->半角字符串互转 JavaScript

 

将半角转换成全角

 

function SBC(text) {
    return text.replace(/[\x20-\x7e]/g, function($0) {
        return $0 == " " ? "\u3000" : String.fromCharCode($0.charCodeAt(0) + 0xfee0);
    });
}

 

 

function n2c(n) {
    var s= n.toString().split(''), 
    r='';
    for (var i=0, length= s.length; i< length; i++) {
        r+='0123456789'.split('')[s[i]];
    }
    return r;
}

 

 

全角转换成半角

function dbc2sbc(s){
     return s.replace(/[\uff01-\uff5e]/g,
            function(a){
                  return String.fromCharCode(a.charCodeAt(0)-5248);
            }
      ).replace(/\u3000/g," ");
}

 

 

 

你可能感兴趣的:(JavaScript)