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

138
Vistas
Javascript regex (x|y) - Can it be specified by array.join('|')?

I'm trying to parse text obtained from the Wikipedia API.

In detail, I'm trying to parse the wikitext of a whole wiki page into separate sections. On wikipedia, sections are divided by titles enclosed by 2-5 equal symbols (e.g. == TITLE ==). So the whole wikitext of a page has a bunch of this, and I was thinking I'd be able to get the content of each section using split() and regex. Here's my naive try:

let wikitext = text; // Obtained from the API
let sectiontitles = [sectiontitle1, sectiontitle2...]; // Obtained from the API
let sectionsPiped = sectiontitles.join('|');
let regex = new RegExp(`\\={2,5}\\s*(${sectionsPiped})\\s*\\={2,5}`, 'g');
console.log(wikitext.split(regex));

This worked, but not the way I expected. Let's say the wikitext has the following contour (replacing \n with actual linebreaks for readability's sake):

== Section 1 ==
SECTION-1-TEXT
=== Section 1.1 ===
SECTION-1.1-TEXT
=== Section 1.2 ===
SECTION-1.2-TEXT

Then the code above returns:

0: ''
1: 'Section 1'
2: 'SECTION-1-TEXT'
3: 'Section 1.1'
4: 'SECTION-1.1-TEXT'
5: 'Section 1.2'
6: 'SECTION-1.2-TEXT'

but I was expecting:

0: ''
1: 'SECTION-1-TEXT'
2: 'SECTION-1.1-TEXT'
3: 'SECTION-1.2-TEXT'

I guess I'm doing something wrong with the (x|y) regex, so I need your help with this.

Note that API:Parsing_wikitext can parse a single section, but not multiple sections (for this reason I'd have to iterate API requests if I try to do this with the API, but I want to avoid this because the code will otherwise be slow). And in the end, I need to get an array of the contents of each section INCLUDING the title headers like so:

0: ''
1: '== Section 1 ==\nSECTION-1-TEXT'
2: '=== Section 1.1 ===\nSECTION-1.1-TEXT'
3: '=== Section 1.2 ===\nSECTION-1.2-TEXT'

I can do this by adding the headers to the second last array above after spliting the whole wikitext, but is there any easier way to do this? Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
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