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

166
Views
Javascript: Regex to convert a numbered list string to an array of items

I am trying to convert a numbered list into an array of items. This is what I have so far:

let input = `1. This is a text\n    where each item can span over multiple lines\n  1.1 this is another item\n 1.2another item\n  2. that I want to\n    extract each seperate\n    item from\n    3. How can I do that?`;

let regex = /(\d+\.\d+|\d+)\s(.*)/g;
let matches = input.match(regex);
console.log(matches);

This only produces the following output:

"1.1 this is another item"

What I would like is something like this:

"1. This is a text"
"1.1 this is another item"
"1.2another item"
...and so on

Why is it matching only one item out of this string? What am I doing wrong and how can I fix it?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your regex does not foresee a dot after a number when there is no second number following it. It also requires a space after the number, but you have a case where there is no such space. So make it optional.

Also, use the s modified so . also matches newline cha

If a new item can start on the same line, you'll need a look-ahead to foresee where a match must end.

Correction:

let input = `1. This is a text\n    where each item can span over multiple lines\n  1.1 this is another item\n 1.2another item\n  2. that I want to\n    extract each seperate\n    item from\n    3. How can I do that?`;

let regex = /(\d+\.\d*)\s?(.*?)(?=\d+\.|$)/gs;
let matches = input.match(regex);
console.log(matches);

about 4 years ago · Santiago Trujillo Report

0

Another option using a negated character class:

\b\d+\.\D*(?:\d(?!\.)[^.]*)*

Explanation

  • \b\d+\.\ A word boundary, match 1+ digits and a dot
  • \D* Optionally match non digits
  • (?:\d(?!\.)[^.]*)* Optionally match a digit asserting not a dot directly to the right

Regex demo

let input = `1. This is a text\n    where each item can span over multiple lines\n  1.1 this is another item\n 1.2another item\n  2. that I want to\n    extract each seperate\n    item from\n    3. How can I do that?`;

let regex = /\b\d+\.\D*(?:\d(?!\.)[^.]*)*/g;
let matches = input.match(regex);
console.log(matches);

If you want to keep the start of the string into account where the digits and the dot start, you can follow the match by asserting not a digit and dot pattern at the start of the string:

^[^\S\n]*\d+\..*(?:\n(?![^\S\n]*\d+\.).*)*

Regex demo

let input = "1. This is a text with a number 1.2 and 3.\n    where each item can span over multiple lines\n  1.1 this is another item\n 1.2another item\n  2. that I want to\n    extract each seperate\n    item from\n    3. How can I do that?";

let regex = /^[^\S\n]*\d+\..*(?:\n(?![^\S\n]*\d+\.).*)*/gm;
let matches = input.match(regex).map(s => s.trim());
console.log(matches);

about 4 years ago · Santiago Trujillo Report
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!