0

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

2
  • 2
    why don't you give real examples of patterns and what you want returned from them? Commented Sep 10, 2012 at 19:17
  • These are just groups that can be completely separated from each other, they don't have common parts inside them and are complicated of their own. They define separate entities and I don't want to mix them even if it's possible. Commented Sep 10, 2012 at 19:20

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I was afraid that this is the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.