Full size numbers convert to half width Numbers in jQuery / JS

A would like to convert full-width numbers (like 123) to half-width numbers (like 123). I found code to do this in PHP, but not in JS. Can anyone please help? Thank.

function fullWidthNumConvert(fullWidthNum){
    // Magic here....
    ....
    return halfWidthNum;
}

      

+3
javascript jquery


source to share


1 answer


Make a string .replace()

using a regular expression to match the specified characters. Callback

function fullWidthNumConvert(fullWidthNum){
    return fullWidthNum.replace(/[\uFF10-\uFF19]/g, function(m) {
      return String.fromCharCode(m.charCodeAt(0) - 0xfee0);
    });
}


console.log(fullWidthNumConvert("0123456789"));
console.log(fullWidthNumConvert("Or in the middle of other text: 123. Thank you."));
      



+5


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics