resultados[i].value = (((num1 * num2).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
Example:
function calcular(){
var valores1 = document.getElementsByClassName('txt1');
var valores2 = document.getElementsByClassName('txt2');
var resultados = document.getElementsByClassName('result');
for (let i = 0; i < valores1.length; ++i){
let num1 = parseFloat(valores1[i].value);
let num2 = parseFloat(valores2[i].value);
//resultados[i].value = (num1 * num2).toFixed(2);
resultados[i].value = (((num1 * num2).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
}
}
<input class="txt1" value="80.50" name="opcao1" id="opcao1" type="text">
<input class="txt2" value="52" name="opcao2" id="opcao2" type="text">
<input class="result" value="" name="opcao3" id="opcao3" type="text" onclick="calcular()" placeholder="Clique aqui para calcular">
The idea is to combine recursively - with the flag g
(global) - making a positive Lookahead (?=(\d{3})+(?!\d))
- a sequence of 3 digits (\d{3})
since there is no right digit (?!\d)
of this sequence - and other than start or end of string \B
Lookahead is a way of marrying strings that have a particular ending or not. It is used (? = ...) for the positive, ie ending with, and (?! ...) for the negative, ie it does not end with.
A simple example would be Rafael's search followed by Ferreira. If there was Rafael or Rafael Otrocoisa, he would not marry. /Rafael(?= Ferreira)/
On the contrary, in this example, it would only marry Rafael or Rafael Otracoisa, but would not marry Rafael Ferreira: /Rafael(?! Ferreira)