3

thanks for looking at my question.

I have a long list of alternatives that I am trying to match in a regex:

var re = new RegExp('o1|o2|o3|o4|o5|...','g')

The problem that I run into is what happens if o1 is a substring of o2. For example

var re = new RegExp('a|b|c|ab|abc','g')

var s = 'abc'

s.match(re) -> ["a", "b", "c"]

I would like for it to also be able to match the "ab" and "abc". I realize if I change the ordering of the RegExp, I can get it to match the longer string, but I really want to get all matches.

What is the best way to do this? This doesn't necessarily seem like the best (or a good way) of dealing with a long list of alternatives. I thought of testing each alternative with its own regexp, but that seemed less efficient.

Any guidance would be great. Thanks!

3
  • So you want all possible matches of these alternatives? I don't think that's possible with only an expression. Oct 21, 2011 at 6:43
  • Yes, that is what I want. I had used this solution in the past where I had a list of options with no overlap and it seemed to be faster than checking each option in its own regexp or using indexOf. I think because of the potential overlap in alternatives I just need to iterate over the list :-/ Oct 21, 2011 at 6:48
  • Google led me to this page when I was searching for alternatives to regex. Would like to share this very useful link that I eventually found: github.com/VerbalExpressions Mar 28, 2019 at 5:49

3 Answers 3

1

If you have only the long list of alternatives in your RegExp the better way to do it is using the indexOf method of String. Here is the code which outputs indexes of all alternatives in the string:

var alternatives = ['a', 'b', 'c', 'ab', 'abc'],
    s = 'abc, cba',
    i,
    index;

for (i = 0; i < alternatives.length; i++) {
    index = -1;
    do {
        index = s.indexOf(alternatives[i], index+1);
        if (index !== -1) {
            console.log(alternatives[i], index);
        }
    } while (index !== -1);
}
3
1

If you try to match the whole string like "abc" then the Rgex would be:

^(a|b|c|ab|abc)$

But there is maybe an easier way, but to help you, I have to know all "alternatives" you like to check for. Maybe a shorter regex expression is possible.

0

You could setup multiple (capturing groups) to get all matches... You still need to order your alternatives accordingly

Using your example:

var re = /((a)(b))(c)|(a)(b)|a|b|c/
var s5 = 'abc';
var s4 = 'ab';
var s3 = 'a';
var s2 = 'b';
var s1 = 'c';

console.log(s5.match(re)); // ['abc', 'ab', 'a', 'b', 'c', undef, undef]
console.log(s4.match(re)); // ['ab', undef, undef, undef, undef, 'a', 'b']
console.log(s3.match(re)); // ['a', ... undef x 6 ...]
console.log(s2.match(re)); // ['b', ... undef x 6 ...]
console.log(s1.match(re)); // ['c', ... undef x 6 ...]

More info on capturing groups

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.