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

88
Vistas
Remove any of multiple characters at beginning and end of string

I want to remove the following characters: [, ], ", \ if any are at the beginning or end of a string, but not if they are in the middle.

So, if the string is ""\[\]][hello" world "\[\"]], I'd like it to become, hello" world

I can't figure out how to do it. I've tried these and many others and none seems to work:

string.replace(/^[\[+|\]+|\"+|\\+]$,"")
string.replace(/^[\[+\]+\"+\\+]$/,"")

Any help would be much appreciated.

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

0

You're close. You need two alternatives, one for the beginning and another for the end. Each should then be a character set in [] listing all the characters you want to remove.

You also need the g modifier to replace twice in the same string.

let string = '""\[\]][hello" world "\[\"]]';

console.log(string.replace(/^["\[\]\\]+|["\[\]\\]+$/g, ''));

about 4 years ago · Juan Pablo Isaza Denunciar

0

I split the problem into following subproblems:

  1. Find these characters at the start of a string
  2. Find these characters at the end of a string

I came up with following regex-patterns to solve these:

  1. /(^["\[]]+)/g
  2. /["\[]]+$/g

When working with regex, I recommend following site: regexr.com/6d193

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