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

248
Visualizações
How to remove specific letters using regex in javascript

var cartstring = "27,00 - R"

How can I remove spaces and "-" and "R" using only regex (not allowed to use slice etc.)? I need to make strings cartstring1 and cartstring2 which should both be equal to "27,00", first by removing spaces and "-" and "R", and second by allowing only numbers and ",".

cartstring1 = cartstring.replace(/\s/g, "");
cartstring2 = cartstring.replace(/\D/g, "");

Please help me modify these regular expressions to have a working code. I tried to read about regex but still cannot quite get it.
Thank you very much in advance.

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

0

you can just capture just what you are interested in number and comma:

let re = /[\d,]+/g
let result = "27,00 - R".match(re)
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can group the characters you want to remove:

var cartstring = "27,00 - R"
let res = cartstring.replace(/(\s|-|R)/g, "")
console.log(res)

Or alternatively, split the string by a space and get the first item:

var cartstring = "27,00 - R"
let res = cartstring.split(" ")[0]
console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

You are using 2 replacements, one replacing all whitespace chars \s and the other replacing all non digits \D, but note that \D also matches \s so you could omit the first call.

Using \D will also remove the comma that you want to keep, so you can match all chars except digits or a comma using [^\d,]+ in a single replacement instead:

var cartstring = "27,00 - R";
console.log(cartstring.replace(/[^\d,]+/g, ''));

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