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

204
Vistas
How to reverse number groups inside a string in javascript

I have a string and I want the number groups inside be reversed, for example "this is number 123456 and the reverse is 654321" be changed to "this is number 654321 and the reverse is 123456" . how can I achieve that?

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

0

As I searched through net , I finally got to this solution:

function reverseNumberInString(str){
    let numbers = str.match(/\d+/g) || [];
    for(let i = 0 ; i < numbers.length ; i++){
        let x = numbers[i].toString().split("").reverse().join("");
        let s = str
        str = s.slice(0,str.search(numbers[i])) + x + s.slice(str.search(numbers[i])+numbers[i].length)
    }
    return str
}

Might Be Interesting:

The whole problem started with thermal pos printer. I had to send a string of hex codes to it for printing , but in my language (persian/farsi) it would print in reverse , so I had to reverse the hex string. but then numbers would be reverse (and of course English words!!) so I reversed the numbers (and for English letters just change str.match(/\d+/g) to str.match(/\w+/g)) in side the reversed string!

Updated:

I also found out that decimal number have problems too. so I changed the regex part to str.match(/[a-zA-Z0-9.،,\/]+/g)

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