Closed. This question needs to be more focused。它当前不接受答案。
想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
在5个月前关闭。
Improve this question
A想要将全 Angular 数字(例如123)转换为半 Angular 数字(例如123)。我发现代码是在PHP中实现的,而在JS中却没有。有人可以帮忙吗?谢谢。
想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
在5个月前关闭。
Improve this question
A想要将全 Angular 数字(例如123)转换为半 Angular 数字(例如123)。我发现代码是在PHP中实现的,而在JS中却没有。有人可以帮忙吗?谢谢。
function fullWidthNumConvert(fullWidthNum){
// Magic here....
....
return halfWidthNum;
}
最佳答案
使用 .replace()
来匹配字符字符串regular expression。 callback in .replace()
's second argument可以获取匹配字符的字符代码,然后从中减去以获得标准数字的字符代码,然后将其转换回字符串。
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."));
关于javascript - 全 Angular 数字在jQuery/JS中转换为半 Angular 数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42917593/