0

How can I remove the line spaces but without remove the structure of the next string.

    P0:::inicial
    P1:::pregunta
    P2:::pregunta
    P3:::pregunta
    R1:::respuesta
    R2:::respuesta
    R3:::respuesta
    R4:::respuesta
    R5:::respuesta
    R6:::respuesta
    R7:::respuesta
    R8:::respuesta
    R9:::respuesta
    C1:::cotizacion
    C2:::cotizacion
    C3:::cotizacion
    A1:::agregar
    A2:::agregar
                         <------ I want delete this spaces.
    C4:::cotizacion

I try using regex but I have problems when the regex and replace remove all the spaces on my string but I only want delete the spaces without information.

In try some like this:

replace(/[\r\n]+/g, " ");
ASASCED
  • 61
  • 1
  • 15

1 Answers1

1

You may use this regex with anchor and MULTILINE flag:

.replace(/^[ \t]*[\r\n]+/gm, "")

RegEx Demo

  • MULTILINE or m mode makes ^ match start of each line.
  • ^: matches start of a line
  • [ \t]*: Match 0 or more spaces or tabs
  • [\r\n]+: Match 1+ line break characters \r or \n
anubhava
  • 761,203
  • 64
  • 569
  • 643