preg_match in JS using match()

by Roydon DSouza

JavaScript

/*console.log('hello');
var value = 'asd {{test:20}} sdas {{test}} ';
var regex = /{{\w+:\d{1,3}}}|{{\w+}}/;
var res = value.match(/{{\w+:\d{1,3}}}|{{\w+}}/g);

console.log(res);
var match = regex.test(value);
if ( ! match) {
    console.log(value + 'is not a number that has between 1,20 digits');
}
 */
var replacement = [];
replacement[0] = 10.43434;
replacement[1] = 9.34;
console.log(replaceVariables(replacement, '<<get>> <<get2>> is a template'));

function replaceVariables(replacement, text) {
	    var variables = [];
	    //text no null
	    variables = text.match(/<<\w+:\d{1,3}>>|<<\w+>>/g);
	    console.log('placeholders ', variables);
	    var variablesFound = variables ? variables.length : 0;
	    console.log('variables', variables);
	    console.log('here');
	    if (variablesFound > 0) {
	    	console.log('length',variablesFound);
	        //$toReplace = array();
	        for (let key in variables) {
	            console.log('---');
	            var len = 0;
	            var variable = variables[key];
	            //$toReplace[] = $variables[0][0];
	            var matches = variable.match(/\d{1,2}/g);
	            console.log('m', matches);
	            if (matches && matches.length > 0) {
	                console.log("length matched. " + variable);
	                console.log(matches);
	                len = matches[0];
	                console.log('*len', len);
	            } else {
	                console.log(replacement[key]);
                  var replacementString = replacement[key] + ''; //toString() removes floating precision so converting to string this way
	                len = (replacementString).length;
	                console.log('rep*len', len);
	            }
	            console.log("key=" + key + ", len=" + len + " " + replacement[key]);
              replacement[key] = replacement[key] + ''; //toString() removes floating precision so converting to string this way
	            replacement[key] = replacement[key].substr(0, len);
	 ...