如何将所有数字替换为阿拉伯语或波斯语

How to replaceAll the numbers to Arabic or Persian

我正在使用 const 来定义更改,这花了我很长时间。如何使用它们以及如何简要介绍代码?:

const [num0en, num1en, num2en, num3en, num4en, num5en, num6en, num7en, num8en, num9en, percEn] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "%"]
const [num0fa, num1fa, num2fa, num3fa, num4fa, num5fa, num6fa, num7fa, num8fa, num9fa, PercFa] = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "٪"]
const En0 = "0";
const Pe0 = "۰";
const En1 = '1';
const Pe1 = '۱';
const En2 = "2";
const Pe2 = "۲";
const En3 = "3";
const Pe3 = "۳";
const En4 = "4";
const Pe4 = "۴";
const En5 = "5";
const Pe5 = "۵";
const En6 = "6";
const Pe6 = "۶";
const En7 = "7";
const Pe7 = "۷";
const En8 = "8";
const Pe8 = "۸";
const En9 = "9";
const Pe9 = "۹";
const EnPercents = "%";
const PePercents = "٪";
const Rep0 = num0en.replaceAll(En0, Pe0);
const Rep1 = num1en.replaceAll(En1, Pe1);
const Rep2 = num2en.replaceAll(En2, Pe2);
const Rep3 = num3en.replaceAll(En3, Pe3);
const Rep4 = num4en.replaceAll(En4, Pe4);
const Rep5 = num5en.replaceAll(En5, Pe5);
const Rep6 = num6en.replaceAll(En6, Pe6);
const Rep7 = num7en.replaceAll(En7, Pe7);
const Rep8 = num8en.replaceAll(En8, Pe8);
const Rep9 = num9en.replaceAll(En9, Pe9);
const RepPer = percEn.replaceAll(EnPercents, PePercents);
const ReplaceAllNumbers = Rep0&&Rep1&&Rep2&&Rep3&&Rep4&&Rep5&&Rep6&&Rep7&&Rep8&&Rep9&&RepPer
    var post =  "0123456789";
// I need to use it in post below:
document.write(post);

顺便说一下,您会注意到一些错误。还请大家见谅..

使用正则表达式,并使用函数作为替换,这样就可以return对应的替换。这个对应关系可以在一个对象中。

const enToPe = {
    [num0en]: num0fa,
    [num1en]: num1fa,
    ..
};

let result = post.replace(/./g, char => enToPe[char] || char);
console.log(result);

var persianIran = "fa-IR"
var arabicEgypt = "ar-EG"

let num = 1234567890;

document.getElementById("num").innerText = num.toLocaleString(persianIran, {
  style: "decimal",
  useGrouping: false
});

document.getElementById("num1").innerText = num.toLocaleString(arabicEgypt, {
  style: "decimal",
  useGrouping: false
});

num = .42;
document.getElementById("pct").innerText = num.toLocaleString(persianIran, {
  style: "percent",
  useGrouping: true
});

document.getElementById("pct1").innerText = num.toLocaleString(arabicEgypt, {
  style: "percent",
  useGrouping: true
});

num = 1027;
document.getElementById("cur").innerText = num.toLocaleString(persianIran, {
  style: "currency",
  currency: "EUR"
});
document.getElementById("cur1").innerText = num.toLocaleString(arabicEgypt, {
  style: "currency",
  currency: "EUR"
});
div {
  display: inline-block;
  margin: 0 20px
}

div>p:first-child {
  margin-bottom: 8px;
  border-bottom: 1px solid gray;
  font-weight: bold;
}

p {
  margin: 2px;
}
<div>
  <p>Values</p>
  <p>1234567890 as Decimal</p>
  <p>.42 as Percent</p>
  <p>1027 as Currency</p>
</div>
<div>
  <p>Persian-Iran</p>
  <p id="num"></p>
  <p id="pct"></p>
  <p id="cur"></p>
</div>
<div>
  <p>Arabic-Egypt</p>
  <p id="num1"></p>
  <p id="pct1"></p>
  <p id="cur1"></p>
</div>