Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda