JSFiddle - React, Tailwind, and code Playground

by BuddhiP

JavaScript

if (!String.prototype.supplant) {
    String.prototype.supplant = function (o) {
        return this.replace(
            /\{([^{}]*)\}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === 'string' || typeof r === 'number' ? r : a;
            }
        );
    };
}

$(function() {
    var str= '9165678823';
    var regEx = new RegExp("(\\d{3})(\\d{3})(\\d{4})");
    
    var m = regEx.exec(str);
    var mask = "({0}){1}-{2}";
    var res = mask.supplant(m.slice(1));
    console.log(res);
});