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

160
Visualizações
How can I found the index of a array of characters in a string without looping on the array characters

i'm searching a simple way to find the index of one character / string between a list of characters.

Something which looks like

"3.14".indexOf([".",","])
"3,14".indexOf([".",","])

Both should return 1.

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

0

One way:

const testRegex = /\.|,/
const match = testRegex.exec("3.14");
console.log(match && match.index)

It uses a regular expression to search for either character, and uses the exec method on regular expressions which returns an object that has the index of the start of the match.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can loop through the array and return the first character's index whose index is not -1:

function getIndex(str, array){
  for(e of array){
    let index = str.indexOf(e);
    if(index != -1){
      return index;
    }
  }
}

console.log(getIndex("3.14", [".",","]))
console.log(getIndex("3,14", [".",","]))

You can also use Array.reduce:

function getIndex(str, array){
  return array.reduce((a,b) => a == -1 ? a = str.indexOf(b) : a, -1)
}

console.log(getIndex("3.14", [".",","]))
console.log(getIndex("3,14", [".",","]))

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