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
a
,aa
,b
,bb
, etc, but notabab
etc. You need to use something like[ab]*
to fix this. – Bourg