Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
How do I prevent regular rexpressions from replacing the first instance of a pattern match with the same values in later pattern matches?

I am certain there might be something that answers this directly if this is what I'm searching for. Now, I know I need to comment things a little bit better and the style probably could use some work, so be gentle please. If you can just point me to a thread where the same problem has been answered, I'd appreciate it. (I've tried to look for one, but I'm no sure I'm using the right search terms.)

Here is the code:

    // Evaluate 5x style expressions and replace with * ivalue.
    eqvalue = eqvalue.replace(/-?[0-9]*[Xx]/g, eqvalue.match(/-?[0-9]*/) + "*" + ivalue);
    dvalue = dvalue.replace(/-?[0-9]*[Xx]/g, dvalue.match(/-?[0-9]*/) + "*" + ivalue);
    
    // Now do the same for naked polynoials like x^2 + x + 3
    eqvalue = eqvalue.replace(/[Xx]/g, ivalue);
    dvalue = dvalue.replace(/Xx/g, ivalue);

What I am trying to do is to convert a polynomial into a string that I can run though the eval function. Basically, if the user enters 5x^2 + x + 5 into a box, I want this to be replaced with something like 5*2.9 + 2.9 +5.

So far, if I'm entering a string like 5x^2 + 15x + 295, I'm getting 5*2.9 + 5*2.9 + 295, and this i not what I want.

I'm getting this instead: enter image description here

How do I alter the regular expressions to get them to not substitute the first value they run across?

(Also, don't worry. The calculus class is over. I promised the professor not to program a Newton's Method after the course, because he did not like the idea of me writing a Reimann sum's calculator. A B- was the final grade in the course.)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The answer suggested by Jay works fine except for mixed-cases like this "5x^2 + x + 5", in this case after replacement your string will be like this: "5 * x^2 + * x + 5", which is not a valid expression. Do this, to obtain a completely valid expression:

dvalue = dvalue.replace(/(-?[0-9]*)[Xx]/g, "$1*" + ivalue);
dvalue = dvalue.replace(/\+\s*\*/g, "+");
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of using .match(), you can define a matching group using ( and ) in the pattern and refer to its contents with $1 in the replacement:

dvalue = "5x^2 + 15x + x + 295";
ivalue = 2.9;

// add missing multiplication signs before x
dvalue = dvalue.replace(/(-?[0-9]+)[Xx]/g, "$1*x");
// replace x with ivalue
dvalue = dvalue.replace(/[Xx]/g, ivalue);

Result:

"5*2.9^2 + 15*2.9 + 2.9 + 295"

EDIT: Adjusted based on Charchit's feedback.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda