19
I just needed a solution to put points to separate numbers from three to three, backwards.
For example:
1000 => 1.000
100000 => 100.000
10000000 => 1.000.000
In a reply I found in English Stackoverflow, the solution passed was to use this regular expression:
function separarDeTresEmTres(numero)
{
return String(numero).split(/(?=(?:...)*$)/ ).join('.');
}
console.log(separarDeTresEmTres(1000));
console.log(separarDeTresEmTres(1000000));
console.log(separarDeTresEmTres(10000000));
But I didn’t quite understand what the magic was made by /(?=(?:...)*$)/
.
What is causing this regular expression to separate the numbers from three to three, backwards? What is the explanation?
NOTE: I don’t want answers explaining how to separate a number from three to three, because the regular expression I’m using is already doing that. The question here is specifically about how each part of this regular expression works. I do not want the solution of the problem without the explanation of what is happening.
I usually use this tool to ask questions: https://regex101.com/ maybe it will help something.
– Filipe Moraes
@Filipemoraes good tip, I’ll take a look now!
– Wallace Maxters
"In a reply I found in Stackoverflow English" - Could put the link?
– Victor Stafusa
@Victorstafusa edited :p
– Wallace Maxters