カタカナ->ひらがな変換
const katakanaToHiragana = (katakana) => {
return katakana.replace(/[\u30a1-\u30f6]/g, (match) => {
const chr = match.charCodeAt(0) - 0x60;
return String.fromCharCode(chr);
});
};
console.info(katakanaToHiragana('カタカナヴ'));
全角英数->半角英数に変換
const zenkakuEisuToHankaku = (str) => {
return str.replace(/[A-Za-z0-9]/g, (s) => {
return String.fromCharCode(s.charCodeAt(0)-0xFEE0);
});
};
console.info(zenkakuEisuToHankaku('ABCDEfghi12345'));
文字列を半角数字のみに変換
const stringToOnlyNumbers = (str) => {
return zenkakuEisuToHankaku(str).replace(/[^0-9]/g, '');
};
console.info(stringToOnlyNumbers('foo12bar34HOGE56fuga78'));
参考