JSFiddle - React, Tailwind, and code Playground

HTML

<p class="phone">574664076</p>
<p class="phone">574664076</p>

JavaScript

$('.phone').text(function(i, text) {
    var n = (text.length)-6;
    if(n==4){var p=n;}else{var p=5;}
    var regex = new RegExp('(\\d{2})(\\d{'+p+'})(\\d{4})');
    var text = text.replace(regex, "($1) $2-$3");
    return text;
});