Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the | in this regexp not just or the two child expressions?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

So first expression should be

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading