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

127
Visualizações
Get first and last character in string with javascript and create substring

Hello if I have a string that is like

"[[{abc}, {abc}, {abc}]]....blah".

How can I get it to just be

"[{abc}, {abc}, {abc}]"

I want to start with the first "[" and end with the last "]" I tried substring but it only works if the string length never changes.

var newString = oldstring.substring(1) //this starts at the second "[" but how to continue till the last "]"?
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can get the expected string using startIndex as 1 and endIndexas str.length - 1

const str = "[[{abc}, {abc}, {abc}]]";
const newString = str.substring(1, str.length - 1);
console.log(newString);

If you are looking to get the string after the very first [ and before the last ], then you can do as:

const str = "[[{abc}, {abc}, {abc}]]";
const strArr = str.split("");
const newString = str.substring(
  strArr.indexOf("[") + 1,
  strArr.lastIndexOf("]")
);
console.log(newString);

about 4 years ago · Juan Pablo Isaza Relatório

0

Use String.slice:

const str = "[[{abc}, {abc}, {abc}]]";

const result = str.slice(1, -1);

console.log(result)

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