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

137
Visualizações
How to extract certain Character from JS String

Given a String that represents a math operation , i want to extract the operations it contains , for ex : 5+4-3 will return two arrays : Operations = [+,-] and the operands will be [5,4,3]

// grab the input and so stuff when user finishes typing

const selectElement = document.querySelector('.formule');
selectElement.addEventListener('change', (event) => {
  const result = document.querySelector('.result');
  segmentFormule(event.target.value);
});


const segmentFormule = function (value){
// first separate operands from operations
    let myOperands = [];
    const formule = new String(value);
    
    // here i extracted the operands into an array, but i couldnt figure out how to extract the operations , so i just hardcoded Addition operation for now.
    
    myOperands=formule.split("+");
    
    let ColoredFormule = "";
    for (var i = 0 ; i < myOperands.length ; i++ ) {
        ColoredFormule += 
        '<span style="color:Red">'+myOperands[i]+'</span> '+'<span style="color:blue">+</span> ';
    }
    document.querySelector('.result').innerHTML = ColoredFormule;
    
    }
<label for="formuleField">Example input: 56+400+300</label>
</br>
<input type="text" class="formule" name="formuleField">
</br>
<div class="result"></div>

I Couldnt figure out how to extract the operations from the given input, any help is appreciated.

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

0

Use a regular expression to create two arrays using match. The first match will match against all none numbers, the second will match against all the numbers.

If you want integers in your operators array map over the array passing in Number for the callback.

const str = '5.14 + 4 - 36';
const operations = str.match(/[^\d \.]/g);
const operands = str.match(/\d+\.?\d*/g).map(Number);
console.log(operations, operands)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use regex with string.match()
(notice the digits are strings, if you need numbers, you have to convert)

let value = '56.2+400-300/200*.23'

const operatorsRegex = /[+\-/*]/g

let operators = value.match(operatorsRegex)

console.log(operators) // ['+','-','/','*']

const digitsRegex = /(\d+\.?\d*)|(\.?\d+)/g

let digits = value.match(digitsRegex)
console.log(digits) // ["56.2", "400", "300", "200", ".23"]

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