Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda