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

263
Views
How to remove part of string that is in parentheses?

I have a string from which I want to remove the last parentheses "(bob)". So far I use this code to return the value within these parentheses:

const str = "Hello my name is (john) (doe) (bob)";
const result = str.split('(').pop().split(')')[0];
console.log(result);

How would I be able to return the string without these last parentheses?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

const str = 'Hello my name is (john) (doe) (bob)';
const lastIdxS = str.lastIndexOf('(');

console.log(str.slice(0, lastIdxS).trim());

about 4 years ago · Santiago Gelvez Report

0

Source: How to remove the last word in a string using JavaScript

Possibly not the cleanest solution, but if you always want to remove the text behind last parentheses, it will work.

   var str = "Hello my name is (john) (doe) (bob)";
   var lastIndex = str.lastIndexOf("(");

   str = str.substring(0, lastIndex);
   console.log(str);

about 4 years ago · Santiago Gelvez Report

0

You can match the last occurrence of the parentthesis, and replace with capture group 1 that contains all that comea before it:

^(.*)\([^()]*\)

Regex demo

about 4 years ago · Santiago Gelvez 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!