LoginSignup
2
1

More than 1 year has passed since last update.

【javascript】全角数字を半角数字に変換

Last updated at Posted at 2021-07-31

数字に限定すれば変換用のマップを組まなくても良い。

コード

お好きなので

const fullnums = '0123456789';
const reFullnums = new RegExp('['+fullnums+']','g');

const toHalfnums = text => text.replace(reFullnums, m=>fullnums.indexOf(m));

console.log(toHalfnums('あ369あ'));     // 'あ369あ'
console.log(toHalfnums(''));     // '7'
const fullnums = '0123456789';
const toHalfnums = text => text.replace(/[0-9]/g, m=>fullnums.indexOf(m));
const toHalfnums = text => text.replace(/[0-9]/g, m=>'0123456789'.indexOf(m));
2
1
2

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
2
1