I have five different regular expressions with common parts. All parts with ? at the end are optional but the order must remain the same. title1-title6 is where the regular expressions differ from each other.
How can I group these in order to eliminate repetition of the common parts?
Pseudo code follows:
title1 type? column option?
title2 name? type? column option?
title3 name? type? column option?
title4 name? column option?
title5 name? column other
What I have so far is:
(title1 type?|(title2|title3) name? type?|(title4|title5) name?) column option?
And besides the repetitions I can't figure out what's the best way to add other part for the last regular expression.
EDIT
I decided to stick to my initial plan to have all regular expressions separate because of the amount of the variables that I have to extract from them. In case anyone is curious what my solution is:
var blocks = {
name1: /regex1/,
name2: /regex2/,
name3: /regex3/,
...
};
var regex = [
createRegex(['name1', 'name2', 'name3', ...]),
createRegex(['name1', 'name3', 'name4', ...]),
...
];
function createRegex = function (params) {
var regex = '';
for (var i=0; i < params.length; i++) {
var name = params[i];
regex += blocks[name].source;
}
return new RegExp(regex, 'i');
}
This is how I initialize the list of regular expressions and it's not a pseudo code (except for the regexes and their names).