Question

I am currently working on a website that is for Japan and by default, their keyboards use double-byte numerals (\uFF10-\uFF19) and this is causing an issue with validation as well as down the flow when we pass the data on to other services. I am writing a function that replaces the double-byte with single-byte (\u0030-\u0039) before submission.

Currently, my plan was to use a lookup table and swap out number for number but feel this is inefficient when looping over a dozen inputs. I'm confident their is a more efficient method that one of you RegEx gurus can come up with.

Any ideas? Thanks in advance!

Was it helpful?

Solution

This is really just a transliteration problem; and what you're replacing, and what you're replacing it with, is all contiguous. So this will work:

function replaceFullWidthNumerals(s) {
  return s.replace( /[\uFF10-\uFF19]/g,
    function(m){
      return String.fromCharCode( m.charCodeAt() - 0xFEE0 );
    }
  );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top