Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda