<分区>
A 想将全 Angular 数字(例如 123)转换为半 Angular 数字(例如 123)。我发现代码在 PHP 中执行此操作,但在 JS 中没有。有人可以帮忙吗?谢谢。
function fullWidthNumConvert(fullWidthNum){
// Magic here....
....
return halfWidthNum;
}
标签 javascript jquery
<分区>
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 - 在 jQuery/JS 中全 Angular 数字转换为半 Angular 数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917593/
相关文章:
javascript - jQuery 附加似乎在 _.each 语句中幽灵附加
javascript - 使用 Executescript 和 Javascript 合并 NiFi 中的 json 流文件
javascript - JS 中 Date() 背后的代码是什么?
javascript - 我的将单词包裹在跨度中的算法的缺陷在哪里?
javascript - preventDefault() 在拖放中阻止了什么?
javascript - MomentJS,如何忽略用户的本地时区,并使用