1

I replace English number strings to Persian with a function like this :

const e2p = (s) => s.replace(/\d/g, (d) => '۰۱۲۳۴۵۶۷۸۹'[d]);

e2p("211") // --> output is ۲۱۱

Now I want to do the opposite by changing arabic string numbers to english .

I tried this method but it didn't work :

const p2e = s => s.replace(/\d/g, d => '0123456789'[d]);

console.log(p2e('۲۱۱')); // outputs --> ۲۱۱

How can I do this in javascript ?

Mehdi Faraji
  • 2,574
  • 8
  • 28
  • 76

1 Answers1

1

I did it with this method :

const p2e = s => s.replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d))

console.log(p2e('۲۱۱')); // outputs --> 211
Mehdi Faraji
  • 2,574
  • 8
  • 28
  • 76