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

103
Vistas
How to properly escape the characters in this regex construction

I am building a naive rstrip function. And it works properly like this when I hardcode the actual characters:

const rstrip = (s, chars) => s.replace(/[r!]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))

However, if I try and use the chars variable, I have trouble escaping it:

const rstrip = (s, chars) => s.replace(/[chars]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))

What would be the proper way to 'escape' the variable so that the value !r replaces the chars variable in the replace method?

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

0

const rstrip = (s, chars) => s.replace(new RegExp(`[${chars}]+$`, 'g'), "");
console.log(rstrip("Hello!!!!r", '!r'))

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