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 to match the Nth char in a string in javascript?

I want to find a word by the position of its characters:

const wordlist = ['xenon', 'rewax', 'roger', 'bob', 'xylophone'] And, to find xylophone, i'll have:

const charSlotPairs = [{char: "x", slot: 0}, {char: "y", slot: 1}];

so I'm dynamically building the regex (^.{0}[x])(^.{1}[y]) but i think this regex is wrong... How do i find match based on the position of a char in a string?

function (charSlotPairs){
  let regexStr = ""
  charSlotPairs.forEach( pair => {
    regexStr += `(^.{${pair.slot}}[${pair.char}])`
  })

  const regex = new RegExp(`${regexStr}\\w`, 'g')
  return this.filter(  word => !word.match(regex) ) 
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is the way I would solve it instead of using regex:

function (charSlotPairs){
  return !!charSlotPairs.find(pair => 
    a.every( word => 
      word.charAt(pair.slot) == pair.char)
    )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Your query can be soved with the use of loop only.

const wordlist = ['xenon', 'rewax', 'roger', 'bob', 'xylophone'];
const charSlotPairs = [{char: "x", slot: 0}, {char: "y", slot: 1}];

let stringFinder = (StrArr, KeyArr) =>
{
    var match = false;  // Indicator Variable 

    // For Iterating a String Array 
    for (var i of StrArr) 
    {
        // For Iterating the Object Array for each Key  
        for (var x of KeyArr) 
        {
            /*
              |If Conditon fails, match will set to false and Loop will Break
              |Otherwise Loop will continue
            */ 
            if (i.charAt(x.slot) != x.char) 
            {
                match = false;
                break;
            }
            else
            {
                match = true;
            }
        }

        /*
          |If the Inner Loop is completed and match is true => Success  
          |If the Inner Loop is completed and match is false => Failed  
        */ 
        if (match) 
        {
            return i;
        }
    }
    return false;
}

console.log(stringFinder(wordlist, charSlotPairs));
about 4 years ago · Juan Pablo Isaza Relatório

0

I ended up filtering the list of words and then using .every() to make sure they all pass the filter

Array.prototype.findWordsWithLettersInSlots = function (letterSlotPairs){
  if( letterSlotPairs.length === 0 ) return this
  if( Object.keys(letterSlotPairs).length === 0 ) return this

  return this.filter( word => {
    return letterSlotPairs.every( (pair) => {
      return word.charAt(pair.slot) === pair.letter 
    })
  })
}
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