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

285
Visualizações
How to get "m" meters and "km" from string using regex?

I was trying to get integer from below string and convert it again replace it . Need regex pattern to get mtr, km array from string. Little help will be highly appreciated.

let stringText = "Please continue for 290 m. And then take left and continue for 1.2 km.";
console.log(stringText);
let mtr_pattern = "";
let kn_pattern = "";
let numArr = stringText.match(/\d+\.\d+|\d+\b|\d+(?=\w)/g) || []);
numArr = numArr.filter(n => n != '.');
console.log("numArry", numArr);

output should be : Please continue for 0.18 miles. And then take left and continue for 0.74 miles

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

0

You could take the numerical value and the unit. Then convert the value to miles.

let
    km2miles = km => km * 0.621371,
    string = "Please continue for 290 m. And then take left and continue for 1.2 km.",
    result = string.replace(
        /(\d+\.?\d*) (k?m)/g,
        (_, a, b) => km2miles(b === 'km' ? a : a / 1000).toFixed(2) + ' miles'
    );

console.log(string);
console.log(result);

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