如何在文本输入中仅将阿拉伯语中的数字转换为英语? [英] How do I convert only numbers from arabic to english in text input?

查看:96
本文介绍了如何在文本输入中仅将阿拉伯语中的数字转换为英语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此功能仅替换文本输入中的阿拉伯数字:



 function parseArabic(){
var yas =0123456789;
yas = Number(yas.replace(/ [0123456789] / g,function(d){
return d.charCodeAt(0) - 1632;
})。replace(/ [0123456789 ] / g,function(d){return d.charCodeAt(0) - 1776;})
);
alert(yas);
}





警告的yas是0123456789,这很棒。现在如果我使用yas =0123456789 g,NaN会收到警报。所以这个功能适用于我们只有阿拉伯数字的情况。所以我需要一个像这样的函数来替换所有阿拉伯数字的值,但其他字符应该保持原样...



我试过的:



我不想单独替换每个阿拉伯数字,例如:replace(0 by 0)和

replace(1 by 1)...

解决方案

删除 Number 函数并尝试



Quote:

所以我需要一个像这样的函数来替换所有的阿拉伯数字值,但其他字符应保持原样



  var  yas =   0123456789 g; 
yas = yas.replace(/ [0123456789] / g, function (d){ return d.charCodeAt( 0 ) - 1632 ;})
.replace(/ [0123456789 ] / g, function (d){ return d.charCodeAt( 0 ) - 1776 ;});
alert(yas); // 0123456789 g


I am trying to use this function to replace only Arabic numbers in text input:

function parseArabic(){ 
     var yas ="٠١٢٣٤٥٦٧٨٩";
     yas = Number(yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
         return d.charCodeAt(0) - 1632;                
         }).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
     );
     alert(yas);
}



The alerted yas is "0123456789" which is great. Now if I use yas ="٠١٢٣٤٥٦٧٨٩ g", NaN is alerted . So this function works in case we have only Arabic numbers. So I need a function like this one to replace all Arabic numbers by their values but other characters should stay as they are ...

What I have tried:

I don't wan't to replace each arabic number alone, example: replace(٠ by 0) and
replace (١ by 1) ...

解决方案

remove the Number function and try

Quote:

So I need a function like this one to replace all Arabic numbers by their values but other characters should stay as they are


var yas = "٠١٢٣٤٥٦٧٨٩ g"; 
    yas = yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) { return d.charCodeAt(0) - 1632; })
        .replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; });
    alert(yas);// "0123456789 g"


这篇关于如何在文本输入中仅将阿拉伯语中的数字转换为英语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆