I have this task:
sum all numbers in string and perform multiplication
input: "3 chairs, 2 tables, 2*3 forks" result: 11
I already have regular expression to do this:
eval(str.match(/(\d[\d\.\*]*)/g).join(' + '))
But I want to add option to ignore numbers inside brackets “()”
input: "2 chairs, 3 tables (1 broke)" result: 5
How to do it?
Regular expressions were always pain for me 🙁
One simple way is to do a first pass and remove all parenthesized expressions.
This is not super efficient but it does the job.
I should note that this does not handle nested parentheses, but it doesn’t seem like that is required here.