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

136
Vistas
How do you access the groups of match/matchAll like an array?

Here's what I would like to be able to do:

function convertVersionToNumber(line) {
  const groups = line.matchAll(/^# ([0-9]).([0-9][0-9]).([0-9][0-9])\s*/g);

  return parseInt(groups[1] + groups[2] + groups[3]);
}

convertVersionToNumber("# 1.03.00")

This doesn't work because groups is an IterableIterator<RegExpMatchArray>, not an array. Array.from doesn't seem to turn it into an array of groups either. Is there an easy way (ideally something that can fit on a single line) that can convert groups into an array?

The API of that IterableIterator<RegExpMatchArray> is a little inconvenient, and I don't know how to skip the first element in a for...of. I mean, I do know how to use both of these, it just seems like it's going to add 4+ lines so I'd like to know if there is a more concise way.

I am using typescript, so if it has any syntactic sugar to do this, I'd be happy to use that.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

1) matchAll will return an Iterator object Iterator [RegExp String Iterator]

result will contain an Iterator and when you use the spread operator It will give you all matches. Since it contains only one match so It contains a single element only.

[ '# 1.03.00', '1', '03', '00', index: 0, input: '# 1.03.00', groups: undefined ]

Finally, we used a spread operator to get all value and wrap it in an array

[...result]

function convertVersionToNumber(line) {
  const result = line.matchAll(/^# ([0-9]).([0-9][0-9]).([0-9][0-9])\s*/g);
  const groups = [...result][0];
  return parseInt(groups[1] + groups[2] + groups[3]);
}

console.log(convertVersionToNumber("# 1.03.00"));

Since you are using regex i.e /^# ([0-9]).([0-9][0-9]).([0-9][0-9])\s*/

enter image description here

2) If there are multiple matches then yon can spread results in an array and then use for..of to loop over matches

function convertVersionToNumber(line) {
  const iterator = line.matchAll(/# ([0-9]).([0-9][0-9]).([0-9][0-9])\s*/g);
  const results = [...iterator];
  for (let arr of results) {
    const [match, g1, g2, g3] = arr;
    console.log(match, g1, g2, g3);
  }
}

convertVersionToNumber("# 1.03.00 # 1.03.00");

Alternate solution: You can also get the same result using simple match also

function convertVersionToNumber(line) {
  const result = line.match(/\d/g);
  return +result.join("");
}

console.log(convertVersionToNumber("# 1.03.00"));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You do not need .matchAll in this concrete case. You simply want to match a string in a specific format and re-format it by only keeping the three captured substrings.

You may do it with .replace:

function convertVersionToNumber(line) {
  return parseInt(line.replace(/^# (\d)\.(\d{2})\.(\d{2})[\s\S]*/, '$1$2$3'));
}
console.log( convertVersionToNumber("# 1.03.00") );

You may check if the string before replacing is equal to the new string if you need to check if there was a match at all.

Note you need to escape dots to match them as literal chars.

The ^# (\d)\.(\d{2})\.(\d{2})[\s\S]* pattern matches

  • ^ - start of string
  • # - space + #
  • (\d) - Group 1: a digit
  • \. - a dot
  • (\d{2}) - Group 2: two digits
  • \. - a dot
  • (\d{2}) - Group 3: two digits
  • [\s\S]* - the rest of the string (zero or more chars, as many as possible).

The $1$2$3 replacement pattern is the concatenated Group 1, 2 and 3 values.

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