JSFiddle - React, Tailwind, and code Playground

by Kleber Mota

JavaScript

var basicRegexLength = function(regex){
    var i, l, n,
        result = '',
        regex = regex.replace(/(\[0-9\]|\[A-Z\])/gi, '');
    for (i = 1; i < 10; i++) {
        regex = regex.replace( new RegExp('\\{' + i + '\\}', 'g'), Array(i+1).join('.') );
    }
    return regex.length;
}

console.log( basicRegexLength("[0-9]{3},[0-9]{2}") ); // should return 6
console.log( basicRegexLength("[0-9]{4},[0-9]{2}") ); //  should return 7
console.log( basicRegexLength("[0-9]{2}.[0-9]{3}.[0-9]{3}/[0-9]{4}-[0-9]{2}") );  //  should return 17
console.log( basicRegexLength("[0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}") ); //  should return 14
console.log( basicRegexLength("[0-9]{2}/[A-Z]{2}/[0-9]{4}") ); //  should return 10