How to simply achieve inclusive or in regex?

Viewed 221

Is there a way to represent inclusive or (that is to match A, B or AB)

(assuming A, B are regex sub-expressions)

that is simpler than this regex ((?:AB)|(?:A|B)) and this ((?:AB?)|(?:B)) ?


I mean, is there a kind of built in operator that performs the same action?

If not, why regex lack such a crucial operator? or is it just me who think that it's important?

1 Answers

Why haven't you mentioned AB|A|B?

const regex = /AB|A|B/;
console.log(
  regex.test('AB'),
  regex.test('A'),
  regex.test('B')
);

Related