simo
simo

Reputation: 15478

Stuck with javascript regular expression grouping

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).

Upvotes: 0

Views: 82

Answers (2)

Diode
Diode

Reputation: 25135

This may not be the exact solution that you are searching for. But you can write a function as shown below to achieve what you need

function getRegExp() {
    var regExp = "";
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i]) {
            if (typeof(arguments[i]) == "string") {
                regExp += "(" + arguments[i] + ")";
            } else if (arguments[i].length) {
                regExp += "(" + arguments[i].join("|") + ")";
            }
        }
    }
    return new RegExp(regExp);
}

var regExp = getRegExp(["title1", "title2", "title3", "title4", "title5"], " ", "name?", " ", "type?", " ", "column", " ", ["option?", "other"]);
console.log(regExp);
regExp.test("title1 name type column option");

Upvotes: 1

driangle
driangle

Reputation: 11779

I would use this expression, only a few of the expressions have common parts.

^((title1\s+(type\s)?column(\soption)?)|((title2|title3) (name\s)?(type\s)?column(\soption)?)|(title4(\sname)?\s+column(\soption)?)|(title5(\sname)?\s+column other))$

Upvotes: 0

Related Questions