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

217
Visualizações
How to get the position of a specified repeated character in a string?

Let's say I have this string:

test (string) (10)

How can I get the position of the third parenthesis (second opening)?

This one:

enter image description here

P.S. In real situations I don't control how many characters are inside the parenthesis, so I can't just count them backwards to get a position. I need the index of the third parenthesis wherever it is.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use a regular expression that matches everything up to the third parenthesis, then get its length.

str = 'test (string) (10)';
match = str.match(/^[^(]*\([^)]*\)[^(]*\(/);
if (match) {
  console.log(match[0].length-1);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

The easiest way is definitely not the most performant way.

If performance isn't an issue, I'd personally use split.

var chunks = "test (string) (10)".split("(")

pos = chunks[0].length + chunks[1].length

You may have to add 1 to the formula above or something like that. I don't have time get it just right ;)

about 4 years ago · Juan Pablo Isaza Relatório

0

Use indexOf twice, first to get the position of the first closing parentheses, then to get the next opening one:

let s = 'test (string) (10)'
    
let position = s.indexOf('(', s.indexOf(')'))
    
console.log(position)

Note that position will be zero-based, add 1 if necessary.

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