JSFiddle - React, Tailwind, and code Playground

by klaascuvelier

HTML

<div id="output"></div>Update

JavaScript

function test(content)
{
    var expr = new RegExp('(<!-- IF ["a-zA-Z0-9_.!=<>\\s]+ -->[\\s\\S]*?)+' +
                          '(<!-- ELSE IF ["a-zA-Z0-9_.!=<>\\s]+ -->[\\s\\S]*?)?' + 
                          '(<!-- ELSEIF ["a-zA-Z0-9_.!=<>\\s]+ -->[\\s\\S]*?)?' + 
                          '(<!-- ELSE -->[\\s\\S]*?)?' 
                          + '<!-- ENDIF -->', 'im');

            var results = expr.exec(content);
            while (results != null)
            {
                //console.log(results);
                var i, len, code = '';
                for (i = 1, len = results.length; i < len; i++)
                {
                    var statement = results[i];
                    if (typeof statement !== 'undefined')
                    {
                        // position for splitting into condition and output
                        var position = statement.indexOf('-->');
                        var condition = statement.substr(4, position - 5).trim().replace('AND', '&&').replace('OR', '||').replace('ELSEIF', 'else if')
                        var output = statement.substr(position + 3).trim();
                     
                        var conditionElseIf = condition.substr(0, 7).toLowerCase() === 'else if';
                        var conditionElse = condition.substr(0, 4).toLowerCase() === 'else';
                     
                        position = conditionElseIf ? 7 : condition.indexOf(' ');
                        var operation = position === -1 ? '' : condition.substr(position).trim();
                        
                        // there is no lookbehind in JS, so also fetch char in front of regexp
                        var variablesExpr = new RegExp('(.?)([a-z0-9_\-]+)(?!")', 'gi');
                        var variables = variablesExpr.exec(operation)
                           
                        while (variables !== null)
                        {
                            if (variables[1] !== '"')
            ...