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

340
Vistas
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 Respuestas
Responde la pregunta

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 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