JSFiddle - React, Tailwind, and code Playground
by Adambasszer
JavaScript
function createGroupingRegex(pattern) {
var regexObj = {};
var elasticExp = "(";
var i;
var capturingGroupNr = 1;
for (var i = 0; i < pattern.length; i++) {
var term = pattern[i];
if (!term.negated && pattern[i + 1] && !pattern[i + 1].negated) {
//not last element, not negated, next one is also not negated
if(pattern[i-1] && !pattern[i-1].negated) {
elasticExp += '_' + term.stepId + '_.*';
}
} else if (!term.negated && !pattern[i + 1]) {
//last pattern element is not negated
elasticExp += '_' + term.stepId + '_';
} else if (!term.negated && pattern[i + 1] && pattern[i + 1].negated) {
//pattern element is not negated, but the next one is negated
var notNegatedStepIdString = "_" + term.stepId + "_";
capturingGroupNr++;
elasticExp += "(?:";
var negatedRepeation = howManyNegatedAfter(pattern, i, 0);
var nextNotNegatedStepIdString = pattern[i + negatedRepeation + 1] ? "_" + pattern[i + negatedRepeation + 1].stepId + "_" : "";
console.log(negatedRepeation);
for (var j = i + 1; j <= i + negatedRepeation; j++) {
elasticExp += notNegatedStepIdString + ".*";
elasticExp += "_" + pattern[j].stepId + "_" + ".*";
elasticExp += nextNotNegatedStepIdString + "|";
}
i += negatedRepeation - 1;
elasticExp += "(" + notNegatedStepIdString + ".*" + nextNotNegatedStepIdString + ")";
elasticExp += ")";
if(i + negatedRepeation !== pattern.length - 1){
elasticExp += ".*";
}
}
}
elasticExp += ')';
regexObj.elasticExp = elasticExp;
regexObj.capturingGroupNr =...