Implemention of regex for float and int Bangla Numbers using js

Viewed 160

I'm facing a problem. I want to validate Bangla int and float numbers using RegEx. I am using the following RegEx

([০-৯][০-৯]*$)

But it's not working on ২৬৭

And again, How I would implement floating bangla numbers (For example:৬৭.৫৬) supported regex once which will select int and float Bangla Numbers. Please somebody help me. (It's for my learning purposes only)

2 Answers

You may use the following to match simple integer or float Bengali numbers:

/[০-৯]+(?:\.[০-৯]+)?/
/[\u09E6-\u09EF]+(?:\.[\u09E6-\u09EF]+)?/

You may validate them (the whole string should match) with

/^[০-৯]+(?:\.[০-৯]+)?$/
/^[\u09E6-\u09EF]+(?:\.[\u09E6-\u09EF]+)?$/

See the regex demo.

Details:

  • ^ - start of string
  • [\u09E6-\u09EF]+ - one or more Bengali digits
  • (?:\.[\u09E6-\u09EF]+)? - an optional occurrence of a . and one or more Bengali digits
  • $ - end of string.

Try this:

regex = /[০-৯]+(\.[০-৯]*)?$/
console.log(regex.test("২৬৭"))

Maybe that was a little bit confusing. Let's break the regex /[০-৯]+(\.[০-৯]*)?$/ down.

First part - `[০-৯]+`

This means that it will match 0-9, one or greater number of times.

Second part - `\.[০-৯]*`

This means that it will match a decimal point ., along with zero or greater numbers.

Third part - `(\.[০-৯]*)?`

The brackets just mean that you are grouping it. The question mark makes it optional.

Fourth part - `$`

Matches to the end of the string.

Putting that together and summarizing: First it matches for a string of numbers. After it encounters a non-number, it checks if it is a decimal point. If it is, then it matches the decimal, and however many numbers come after it (which might be none, meaning that the decimal is there for significant figure purposes, or for another reason).

Related