Home > Software engineering >  Why does the | in this regexp not just or the two child expressions?
Why does the | in this regexp not just or the two child expressions?

Time:05-05

Here is a snippet of the problem I am running into

console.log(/^true|false|(([- /*^]?\d (\.\d )?)*)$/.test('foo'))
console.log(/^true|false$/.test('foo'))
console.log(/^([- /*^]?\d (\.\d )?)*$/.test('foo'))

This works fine in other regexp testers, but js consistently fails this test. Can anyone explain what I'm doing wrong here. Thank you

CodePudding user response:

^a|b|c$ means ^a or b or c$

So first expression should be

console.log(/^(true|false|(([- \/*^]?\d (\.\d )?)*))$/.test('foo'))
  • Related