Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!