billrogers
Feb 2nd, 2001, 08:31 AM
I am trying to parse a polynomial, so it could be of the form
1) 3-4x+5x^2
2) 3+5x^2-4x
3) 4x-3+5x^2
4) 4x+5x^2-3
5) 5x^2-3+4x
6) 5x^2+4x-3
Oh also there is the case that something is not there for example 3-4x so it will never find the x^2 coeffiecent, but match returns a null then so that is ok, cuz I can handle that later.
What I am doing is using the following: (on logs[1] is just the polynomial string)
c[0] = logs[1].match(/(\+|\-|^)(\d+)[^x]/);
c[1] = logs[1].match(/(\+|\-|^)(\d*)x[^^]/);
c[2] = logs[1].match(/(\+|\-|^)(\d*)x\^/);
Does anyone know why this doesnt work for all the above cases (1-6)?
1) 3-4x+5x^2
2) 3+5x^2-4x
3) 4x-3+5x^2
4) 4x+5x^2-3
5) 5x^2-3+4x
6) 5x^2+4x-3
Oh also there is the case that something is not there for example 3-4x so it will never find the x^2 coeffiecent, but match returns a null then so that is ok, cuz I can handle that later.
What I am doing is using the following: (on logs[1] is just the polynomial string)
c[0] = logs[1].match(/(\+|\-|^)(\d+)[^x]/);
c[1] = logs[1].match(/(\+|\-|^)(\d*)x[^^]/);
c[2] = logs[1].match(/(\+|\-|^)(\d*)x\^/);
Does anyone know why this doesnt work for all the above cases (1-6)?