25
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめての記事投稿

[JavaScript] 全角・半角の文字列を変換する方法

Last updated at Posted at 2023-07-08

文字列の入力を全角または半角に変換して統一することは、非常に役立ちます。
JavaScriptによって、数字、アルファベット、カタカナの全角半角変換を簡単に実現できます。
ここでは、その実装方法についてメモしたいと思います。

全角 → 半角 (英数字)

function toHalfWidth(str) {
  // 全角英数字を半角に変換
  str = str.replace(/[A-Za-z0-9]/g, function(s) {
    return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
  });
  return str;
}

>> console.log(toHalfWidth("abc123")); // "abc123"

全角 → 半角 (カタカナ)

function kanaFullToHalf(str){
    let kanaMap = {
        "": "ガ", "": "ギ", "": "グ", "": "ゲ", "": "ゴ",
        "": "ザ", "": "ジ", "": "ズ", "": "ゼ", "": "ゾ",
        "": "ダ", "": "ヂ", "": "ヅ", "": "デ", "": "ド",
        "": "バ", "": "ビ", "": "ブ", "": "ベ", "": "ボ",
        "": "パ", "": "ピ", "": "プ", "": "ペ", "": "ポ",
        "": "ヴ", "": "ヷ", "": "ヺ",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "ソ",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "",
        "": "", "": "", "": "", "": "", "": "", "": "",
        " ": " "
    };
    let reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
    return str.replace(reg, function(s){
        return kanaMap[s];
    }).replace(/゛/g, '').replace(/゜/g, '');
}

>> console.log(this.kanaFullToHalf("アイウエオ")); // "アイウエオ"
>> console.log(this.kanaFullToHalf("テスト クレジットカード")); // "テスト クレジットカード"

半角 → 全角 (英数字)

function toFullWidth(str) {
  // 半角英数字を全角に変換
  str = str.replace(/[A-Za-z0-9]/g, function(s) {
    return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
  });
  return str;
}

>> console.log(toFullWidth("abc123")); // "abc123"

半角 → 全角(カタカナ)

function kanaHalfToFull(str) {
    var kanaMap = {
        'ガ': '', 'ギ': '', 'グ': '', 'ゲ': '', 'ゴ': '',
        'ザ': '', 'ジ': '', 'ズ': '', 'ゼ': '', 'ゾ': '',
        'ダ': '', 'ヂ': '', 'ヅ': '', 'デ': '', 'ド': '',
        'バ': '', 'ビ': '', 'ブ': '', 'ベ': '', 'ボ': '',
        'パ': '', 'ピ': '', 'プ': '', 'ペ': '', 'ポ': '',
        'ヴ': '', 'ヷ': '', 'ヺ': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', 'ソ': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '',
        '': '', '': '', '': '', '': '', '': '', '': ''
    };

    var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
    return str.replace(reg, function (match) {
        return kanaMap[match];
    }).replace(/゙/g, '').replace(/゚/g, '');
};

>> console.log(kanaHalfToFull("アプリケーション")); // "アプリケーション"

カタカナ→ひらがな

function kanaToHira(str) {
  return str.replace(/[\u30a1-\u30f6]/g, function(s){
    return String.fromCharCode(s.charCodeAt(0) - 0x60);
  });
}

>> console.log(kanaToHira("カタカナ")); // "かたかな"

ひらがな→カタカナ

function hiraToKana(str) {
  return str.replace(/[\u3041-\u3096]/g, function(s){
    return String.fromCharCode(s.charCodeAt(0) + 0x60);
  });
}

>> console.log(hiraToKana("ひらがな")); // "ヒラガナ"
25
13
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?