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

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

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

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

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