I want to use a regular expression to format a number inside an input when I type it.
My problem: since I use groups to format a number, it is formatted only when the string matches the regular expression. Here is an example:
Full number: 12312312312| Formatted format is as follows: 123.123.123-12.
If I type 1231231, for example, it does not format 123.123.1, as I expected, only if I type the entire number.
This is my function:
format(value){
return value.replace(/(\d{3})(\d{3})(\d{3})(\d+)/, "\$1.\$2.\$3-\$4");
}
Is there a way to make other groups options?
source
share