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

255
Vistas
Remove all but numerical values from a String in Javascript

I'm learning JS and want to know what's the most efficient way to extract Integer values from Strings like below:

const String1 = '122,730 views'
const String2 = '2,990 likes'

Output:

122730
2990

Is there a JS function to remove all but numerical values from a String? In this case, parseInt() would just return 122 and 2.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

JavaScripts parseInt() function does not know any thousands separator.
You can just simply replace them beforehand.

const String1 = '122,730 views';
const Value1 = parseInt(String1.replace(/,/g, ""));

console.log(Value1);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try to use Regex like this:

const regex = /[0-9+]/gm;
const str = `122,730 views`;
let m;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  // The result can be accessed through the `m`-variable.
  m.forEach((match, groupIndex) => {
    console.log(`Found match, group ${groupIndex}: ${match}`);
  });
}

P.S. I'm sorry I didn't comment, but It's impossible, because of my low reputation.

about 4 years ago · Juan Pablo Isaza Denunciar

0

function retnum(str) {
  var num = str.replace(/[^0-9]/g, '');
  return parseInt(num, 10);
}

console.log('122,730 views'.replace(/[^0-9]/g, ''));
console.log(retnum('122,730 views'));

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