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

338
Views
How can I convert a line text divided by numbers in an ordered list using JavaScript?

I have the following text returned from an API in a single line:

1. Choose a target. Pick a target within your attack's range: a creature, an object, or a location. 2. Determine modifiers. The GM determines whether the target has cover and whether you have advantage or disadvantage against the target. In addition, spells, special abilities, and other effects can apply penalties or bonuses to your attack roll. 3. Resolve the attack. You make the attack roll. On a hit, you roll damage, unless the particular attack has rules that specify otherwise. Some attacks cause special effects in addition to or instead of damage.

What I am looking for is to make an ordered list based on this text separated by the digits. I have this code trying to make this work:

html = html.replace(/\d\.([\s\w]+)/g, "<li>$1</li>");

But it gives me this as a result:

•

Choose a target. Pick a target within your attack's range: a creature, an object, or a location.

•

Determine modifiers. The GM determines whether the target has cover and whether you have advantage or disadvantage against the target. In addition, spells, special abilities, and other effects can apply penalties or bonuses to your attack roll.

•

Resolve the attack. You make the attack roll. On a hit, you roll damage, unless the particular attack has rules that specify otherwise. Some attacks cause special effects in addition to or instead of damage.

What I am trying to come as a result would be:

  1. Choose a target. Pick a target within your attack's range: a creature, an object, or a location.
  2. Determine modifiers. The GM determines whether the target has cover and whether you have advantage or disadvantage against the target. In addition, spells, special abilities, and other effects can apply penalties or bonuses to your attack roll.
  3. Resolve the attack. You make the attack roll. On a hit, you roll damage, unless the particular attack has rules that specify otherwise. Some attacks cause special effects in addition to or instead of damage.

Can someone please help me telling what I am doing wrong and a possible solution to make this work? Thank you very much for the help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the fact that <li> elements don't need to be closed explicitly in HTML, and then it is as simple as replacing every number with <li>.

Also make sure that the parent element is an <ol> element ("ordered list"), so you get the numbering instead of the bullets:

let html = "1. Choose a target. Pick a target within your attack's range: a creature, an object, or a location. 2. Determine modifiers. The GM determines whether the target has cover and whether you have advantage or disadvantage against the target. In addition, spells, special abilities, and other effects can apply penalties or bonuses to your attack roll. 3. Resolve the attack. You make the attack roll. On a hit, you roll damage, unless the particular attack has rules that specify otherwise. Some attacks cause special effects in addition to or instead of damage.";

html = html.replace(/\s*\d+\.\s*/g, "<li>");

document.querySelector("ol").innerHTML = html;
<ol></ol>

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