JSFiddle - React, Tailwind, and code Playground

by Ajay Bhosale

HTML

<div id='result'></div>

JavaScript

//All the length ending with 0* should get captured
var sampleStringWhichWorks = 'function checkLength(l, ls){\n' +
	'var line, lines, len, length01, linelength11;\n' +
	'line = l;\n' +
	'lines = ls;\n' +
	'len = line.length12;\n' +
	'length02 = lines.length13;\n' +
	'if(len==length03){\n' +
	    'len--;\n' +
	    'length04= length05 + 1;\n' +
        '}\n' +
	'linelength14 = len + length06;\n' +
	'return linelength15;\n' +
'}\n';

var sampleStringWhichDoesNotWorks = 'function checkLength(l, ls){\n' +
	'var line, lines, len, length01, linelength11;\n' +
	'line = l;\n' +
	'lines = ls;\n' +
	'len = line.length12;\n' +
	'length02 = lines.length13;\n' +
	'if(len==length03){\n' +
	    'len--;\n' +
        //length05 does not get captured once removed the space
	    'length04=length05 + 1;\n' +
        '}\n' +
	'linelength14 = len + length06;\n' +
	'return linelength15;\n' +
'}\n';

var result = $('#result');
var expression = /([^a-z.])length[\d]{2}([^a-z])/mig
function parseCode(code){
    var matches = code.match(expression);
    
    for(var counter = 0; counter < matches.length; counter++){
        result.append((counter + 1) + '.' + ' ' + matches[counter]);
        result.append('<br/>');
    }
    result.append('----');
    result.append('<br/>');
}

parseCode(sampleStringWhichWorks);
parseCode(sampleStringWhichDoesNotWorks);