JavaScriptで全角数字→半角数字(半角数字→全角数字)の変換


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // 全角→半角
  2. function Zen2Han(str) {
  3.   return str.replace(/[0-9]/g, function(str){return String.fromCharCode(str.charCodeAt(0)-65248);});
  4. }
  5.  
  6. //半角→全角
  7. function Han2Zen(str) {
  8.   return str.replace(/\d/g, function(str){return String.fromCharCode(str.charCodeAt(0)+65248);});
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.