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

272
Visualizações
Javascript regex not returning match

I have a simple regex in which I'm trying to extract the leading value including the brackets. But for some reason it returns the entire string every time.

Sample string [1] My well-being if it includes [####] i want to then be able to get the contents of the match results so i can get [4] as a result.

const arr = [
  'I am not valid',
  '[1] My well-being',
  '[1] I conversation about',
  '[3] I role within',
  '[3] I members in the company',
  '[3]I priorities',
  '[3]How other teams',
  '[4] I within a team',
  '[4] I am a peer',
  '[4] I am a label',
  '[4] I am a label'
]

const regex = /^(\[\d+\])(.*)/img;
for (let index = 0; index < arr.length; index++) {
  const name = arr[index];
  const match = name.match(regex);
  console.log(match)
}

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

0

It's because of your use of the g flag.

To quote the documentation:

  • If the g flag is used, all results matching the complete regular expression will be returned, but capturing groups will not.
  • if the g flag is not used, only the first complete match and its related capturing groups are returned. In this case, the returned item will have additional properties as described below.

You can fix this by simply removing g from your regular expression, then name.match() will return capturing groups as well.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you only want the number in the brackets, leave out .* after it.

You also don't need any of the flags. i is for case-insensitive matching, but you don't have any letters. m makes ^ and $ match line endings, but you don't have any multi-line strings. And g is for foring all matches, but since the pattern is anchored there can only be one match.

And since the pattern is now the whole match, you don't need a capture group around it.

const arr = [
  'I am not valid',
  '[1] My well-being',
  '[1] I conversation about',
  '[3] I role within',
  '[3] I members in the company',
  '[3]I priorities',
  '[3]How other teams',
  '[4] I within a team',
  '[4] I am a peer',
  '[4] I am a label',
  '[4] I am a label'
]

const regex = /^\[\d+\]/;
for (let index = 0; index < arr.length; index++) {
  const name = arr[index];
  const match = name.match(regex);
  console.log(match)
}

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