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

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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