Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!