Javascript Adding line break before "-" and after "."

Egidi

I have a field in a postgres database like this:

BACHILLER - Ciencias y Tecnología. - Humanidades y Ciencias Sociales. - Idiomas: inglés y francés. PROYECTOS Y SERVICIOS PEDAGÓGICOS - Aula abierta de apoyo (PROA). - Departamento de Orientación con dos orientadoras: atención individualizada y colectiva de alumnado y familias. - Tutoría coordinada por niveles. - Plan de Normalización Lingüística del Euskera. - AULATICE. INNOVACIÓN Y PROYECTOS DE FORMACIÓN - Página web del centro, dinámica y de apoyo a la enseñanza. - Blogs: Orientación, Psicología, AGENDA 21 ESCOLAR (A21e), Matemáticas, Física, PARADISE, Números, Asociación de Padres y Madres… - AGENDA 21 ESCOLAR (A21e). Participación en la agenda municipal. Certificado de Escuela Sostenible. - Proyecto plurilingüe: ENGLISH PROJECT. - Proyecto del Aula de Aprendizaje de Tareas (AAT): cocina. - Participación activa en los proyectos convocados por el Dpto. de Educación. SERVICIOS - Transporte escolar subvencionado en ESO. - Comedor escolar (ESO). - Acceso a Internet para el alumnado (WI-FI) e Intranet. INSTALACIONES - Enfermería. - Dos bibliotecas informatizadas (ESO y Bachiller). - TV y DVD en aulas. - Cuatro aulas de Informática. - Salón de actos. - Laboratorios de: Física, Química, Ciencias Naturales y Fotografía. - Talleres de: Plástica, Tecnología y Electrotecnia. - Aulas especializadas de: música, arte, euskera, castellano, inglés, AULATICE… - Amplias instalaciones deportivas, cubiertas y al aire libre. Frontón.

I want to use javascript to convert this text into:

BACHILLER
- Ciencias y Tecnología.
- Humanidades y Ciencias Sociales.
- Idiomas: inglés y francés.
PROYECTOS Y SERVICIOS PEDAGÓGICOS
- Aula abierta de apoyo (PROA).
- Departamento de Orientación con dos orientadoras: atención individualizada y colectiva de
alumnado y familias.
- Tutoría coordinada por niveles.
- Plan de Normalización Lingüística del Euskera.
- AULATICE.
INNOVACIÓN Y PROYECTOS DE FORMACIÓN
- Página web del centro, dinámica y de apoyo a la enseñanza.
- Blogs: Orientación, Psicología, AGENDA 21 ESCOLAR (A21e), Matemáticas, Física, PARADISE,
Números, Asociación de Padres y Madres…
- AGENDA 21 ESCOLAR (A21e). Participación en la agenda municipal. Certificado de Escuela
Sostenible.
- Proyecto plurilingüe: ENGLISH PROJECT.
- Proyecto del Aula de Aprendizaje de Tareas (AAT): cocina.
- Participación activa en los proyectos convocados por el Dpto. de Educación.
SERVICIOS
- Transporte escolar subvencionado en ESO.
- Comedor escolar (ESO).
- Acceso a Internet para el alumnado (WI-FI) e Intranet.
INSTALACIONES
- Enfermería.
- Dos bibliotecas informatizadas (ESO y Bachiller).
- TV y DVD en aulas.
- Cuatro aulas de Informática.
- Salón de actos.
- Laboratorios de: Física, Química, Ciencias Naturales y Fotografía.
- Talleres de: Plástica, Tecnología y Electrotecnia.
- Aulas especializadas de: música, arte, euskera, castellano, inglés, AULATICE…
- Amplias instalaciones deportivas, cubiertas y al aire libre. Frontón.

I need to code a little function that makes the trick. I think that inserting a new line break in the oupput text before every "-" and after a "." sign would be enough. It has to be smart enough not to add two line breaks when both requirement are met.

My javascript knowledge is quite limited and i need same help this time...

Regards,

ksbg

The best way to achieve this would probably be using regular expressions. If you want to learn more about regular expressions, I suggest checking out RegexOne.com.

My solution would be:

textblock.replace(/ - /g, "\n- ").replace(/\./g, ".\n").replace(/^\s*\n/gm, "");

First I replace every "-" and every "." with a line break, and afterwords i remove any empty lines that occur when both conditions are met.

JSFiddle link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Force line break before adding string in JS?

From Dev

Adding space before the second line of the Text break

From Dev

Adding or Forcing Line Break after div

From Dev

Adding a line break in JavaScript (full calendar)

From Dev

adding line break into javascript from php

From Dev

adding line break into javascript from php

From Dev

Issue on Adding Horizontal Line Before and After Paragraph

From Dev

MS Word: Adding a line before and after text

From Dev

Adding a line break after certain set of characters in Python

From Dev

How to add a line break before and after a regex in a text file?

From Dev

How to add a line break before and after a regex in a text file?

From Dev

How to not break line on before "?"?

From Dev

Select the last line after last automatic line break with Javascript

From Dev

Insert a line break before a numeric field or before an alphanumeric field that is just after a numeric field

From Dev

Adding line break in NodeAttributes in OmniXML

From Dev

Removing certain text and line breaks then adding a line break after X count of commas PHP

From Dev

how to break line after certain length using javascript

From Dev

Javascript / Forcing a line break after the point if I have one word

From Dev

line break in javascript

From Dev

How can I use CSS to insert a line break after but not before an element?

From Dev

How can I add a break line after the header of a sequence and before the actual sequence?

From Dev

How can I add a break line after the header of a sequence and before the actual sequence?

From Dev

Line break after printing array?

From Java

No line-break after a hyphen

From Dev

No line break after @Override annotation?

From Dev

Textarea line break after ';" character

From Dev

grep for contents AFTER line break

From Dev

Adding an absolute positioned element causes line break

From Dev

Adding a line break in xs:documentation for a xsd schema

Related Related

  1. 1

    Force line break before adding string in JS?

  2. 2

    Adding space before the second line of the Text break

  3. 3

    Adding or Forcing Line Break after div

  4. 4

    Adding a line break in JavaScript (full calendar)

  5. 5

    adding line break into javascript from php

  6. 6

    adding line break into javascript from php

  7. 7

    Issue on Adding Horizontal Line Before and After Paragraph

  8. 8

    MS Word: Adding a line before and after text

  9. 9

    Adding a line break after certain set of characters in Python

  10. 10

    How to add a line break before and after a regex in a text file?

  11. 11

    How to add a line break before and after a regex in a text file?

  12. 12

    How to not break line on before "?"?

  13. 13

    Select the last line after last automatic line break with Javascript

  14. 14

    Insert a line break before a numeric field or before an alphanumeric field that is just after a numeric field

  15. 15

    Adding line break in NodeAttributes in OmniXML

  16. 16

    Removing certain text and line breaks then adding a line break after X count of commas PHP

  17. 17

    how to break line after certain length using javascript

  18. 18

    Javascript / Forcing a line break after the point if I have one word

  19. 19

    line break in javascript

  20. 20

    How can I use CSS to insert a line break after but not before an element?

  21. 21

    How can I add a break line after the header of a sequence and before the actual sequence?

  22. 22

    How can I add a break line after the header of a sequence and before the actual sequence?

  23. 23

    Line break after printing array?

  24. 24

    No line-break after a hyphen

  25. 25

    No line break after @Override annotation?

  26. 26

    Textarea line break after ';" character

  27. 27

    grep for contents AFTER line break

  28. 28

    Adding an absolute positioned element causes line break

  29. 29

    Adding a line break in xs:documentation for a xsd schema

HotTag

Archive