SOURCE

function format_1(str) {
    const re = /(\d{3})(\d{3})(\d{3})/;
    const new_str = str.toString();
    return new_str.replace(re, '$1,$2,$3');
}

function format_2(str) {
    const new_str = str.toString();
    let ans = "";
    let index = 0,
        tmp = 0;
    for (let i = 0; i < new_str.length; i++) {
        index = i + 1;
        ans += new_str[i];
        if (index % 3 == 0 && index != new_str.length) {
            ans += ',';
        }
        tmp++;
    }
    return ans;
}


console.log(1, format_1(123456789));
console.log(2, format_2(123456789));
console 命令行工具 X clear

                    
>
console