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

194
Vistas
Return only numbers from string

I have a value in Javascript as

var input = "Rs. 6,67,000"

How can I get only the numerical values ?

Result: 667000

Current Approach (not working)

var input = "Rs. 6,67,000";
var res = str.replace("Rs. ", "").replace(",","");
alert(res);

Result: 667,000
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This is a great use for a regular expression.

    var str = "Rs. 6,67,000";
    var res = str.replace(/\D/g, "");
    alert(res); // 667000

\D matches a character that is not a numerical digit. So any non digit is replaced by an empty string. The result is only the digits in a string.

The g at the end of the regular expression literal is for "global" meaning that it replaces all matches, and not just the first.

This approach will work for a variety of input formats, so if that "Rs." becomes something else later, this code won't break.

over 4 years ago · Santiago Trujillo Denunciar

0

For this task the easiest way to do it will be to us regex :)

var input = "Rs. 6,67,000";
var res = input.replace(/\D/g,'');
console.log(res); // 667000

Here you can find more information about how to use regex:

https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

I hope it helped :)

Regards

over 4 years ago · Santiago Trujillo Denunciar

0

You can make a function like this

function justNumbers(string) {
  var numsStr = string.replace(/[^0-9]/g, '');
  return parseInt(numsStr);
}

var input = "Rs. 6,67,000";
var number = justNumbers(input);
console.log(number); // 667000

over 4 years ago · Santiago Trujillo 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