How to use 'or' operator in regex properly?
Asked Answered
L

2

5

I am trying Javascript's regular expression.
I understand that '|' is used to or-ing two regular expression.
I created a regex /^a*|b*$/, and I want it to detect any string that contains only charater of 'a' or 'b'.
But when I try /^a*|b*$/.test('c'), it produces true?
What I am missing understading of '|' operator?

Here's my code:

let reg = /^a*|b*$/;
< undefined
reg.test('c');
< true
Livorno answered 22/8, 2018 at 4:7 Comment(2)
Note that even the "corrected" version of this regex will match a, aa, b, bb, etc, but not abab etc. You need to use something like [ab]* to fix this.Bourg
yes, I know that!Blackfish
A
11

| has very low precedence. ^a*|b*$ matches

  • either ^a*
  • or b*$

i.e. either a string beginning with 0 or more 'a's or a string ending with 0 or more 'b's. (Because matching 0 'a's is allowed by the regex, any string will match (because every string has a beginning).)

To properly anchor the match on both sides, you need

/^(?:a*|b*)$/

(the (?: ) construct is a non-capturing group).

You could also use

/^a*$|^b*$/

instead.

Note that both of these regexes will only match strings like aa, bbbbbb, etc., but not aba. If you want to allow the use of mixed a/b characters in a string, you need something like

/^(?:a|b)*$/
Ambulatory answered 22/8, 2018 at 4:12 Comment(1)
@NghĩaNguyễn You should accept the answer if it really helps solve your issue.Bellbottoms
F
1

The OR in your example will split the expression in these alternatives: ^a* and b*$.

You can use groups to delimit the alternatives

Something like

/^(a*|b*)$/

This will match empty strings, strings that contains only a characters and strings that contain only b characters.

If you're looking to match strings that contain both a and b characters, you can use something like:

/^[ab]*$/
Figwort answered 22/8, 2018 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.