JSFiddle - React, Tailwind, and code Playground

HTML

<input type="text" class="telnumber" maxlength="12" />

JavaScript

$('.telnumber').keyup(function() {
    foo = $(this).val().split("-").join(""); // remove hyphens
 
        foo = foo.match(new RegExp('.{1,4}$|.{1,3}', 'g')).join("-");

        $(this).val(foo);
   
    });