Trying to do what I thought was a fairly simple string replacement, but turns out to be more complicated than I thought.
If I have a string like
months + 3 + (startmonths * 3) + months - (months*7) + (monthsend*5)
For clarity, the "formula" I am parsing is user supplied, and can consist of any series of variables names and operators (+*/-) and parens that that a user can come up with. What I will need to do is first replace the variables with numbers and then evaluate the resulting expression.,
what I am looking for is how to replace all occurrences of the words months with, say "12" using string.replace function.
So hopefully the output of the function is
12 + 3 + (startmonths * 3) + 12 - (12*3) + (monthsend*5)"
It seems like I need a regex to avoid replacing in strings like "startmonths", I am maybe under the impression its actually not possible to do in javascript regex because the "lookbehinds" are sparsely supported across modern browsers.
I tried using
[^A-Za-z9-9_](months)(?:[^A-Za-z0-9_])
but that captures the character preceding and following 'months', so I can't use it as a parameter to string.replace.
Is there some workaround, or do I have to forget the replace function and do it "by hand" with find and splice etc?