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

114
Vistas
String match one or more numbers with a # - Javascript / regex

I am trying to match a part of a string with another string. In string one I have a larger amount of text that has the desired string within. In string two I have a part of the desired string with the numbers replaced with '#'.

How can I match any numbers in the same position of the #?

I am guessing that there would be a regex that does this but I can't figure it out.

Example

Str1 = "Adds 2 to 5 Cold Damage Adds 20 to 50 Fire Damage Adds 1 to 100 Lightning Damage"    

Str2 = "Adds # to # Fire Damage" 

res = "Adds 20 to 50 Fire Damage"
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Replace the # with \d+ to match a number there, and convert that to a regular expression.

let Str1 = "Adds 2 to 5 Cold Damage Adds 20 to 50 Fire Damage Adds 1 to 100 Lightning Damage";
let Str2 = "Adds # to # Fire Damage";
let re = new RegExp(Str2.replaceAll('#', '\\d+'));
let res = Str1.match(re);
if (res) {
  console.log(res[0]);
}

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