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

137
Vistas
How to iterate the split result array of a multiline string value in order to reformat certain lines / newlines?

I have a string of data that has line feeds in the middle. For example:

"Product Name \n Product Color \n Product Quantity \n Product Location \n Product Size \n Product Power" 

The Amount of elements in the string could be infinite.

I need to replace the \n with >>>> \n but only when the line numbers are greater than 3 and not the last line. On the last line I need it to say (end)

I have tried map, and I can split and replace, but I'm having trouble iterating through the array to identify the lines that are to be modified. As well as joining them back up once all lines are modified or not.

Input is:

"Retract-A-Banner\n**Template:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n"

Expected Output:

"Retract-A-Banner\n**Template:\nFull Color >>>> \nImprint Information: >>>> \nCL: 488 Special Ops Wing >>>> \n353 SOW Integrated Resilience Optimization Network(end)\n"
over 4 years ago · Santiago Trujillo
5 Respuestas
Responde la pregunta

0

Split by \n and recreate your string with the conditions:

  1. if this is the last one so add (end)
  2. if line is greater than 3, add >>>> \n
  3. otherwise a simple \n will be fine

EDIT:

add a check whether the last string is \n, and remove that value from the array.

const str = "Retract-A-Banner\n**Template:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n";

const splitted = str.split('\n');
let output = '';

if (splitted[splitted.length - 1] === '')
  splitted.length--;
  
for (let i = 0; i < splitted.length; i++) {
  if (i + 1 === splitted.length) {
    output += splitted[i] + ' (end)';
  } else if (i >= 2) {
    output += splitted[i] + ' >>>> \n';
  } else {
    output += splitted[i] + '\n';
  }
}

console.log(output);

over 4 years ago · Santiago Trujillo Denunciar

0

It seems like you want something like this:

var strings = `Retract-A-Banner\nTemplate:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n`.split("\n");
strings.forEach((string,index) => {
    if (index > 2) {
       strings[index] = `>>>>\\n${string}`
    } else {
       index > 0 ? `\\n${string}` : strings[index];
    }
})
var finalString = strings.join("")+"(end)";
console.log(finalString);

Note that I'm escaping your \n in that code with an extra backslash. I cannot tell if that's what you want though.

over 4 years ago · Santiago Trujillo Denunciar

0

How about something like this:

var data =
  "Retract-A-Banner\n**Template:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n";

function convertToLines(entry) {
  return entry.split("\n").filter((val) => val);
}

function constructOutput(lines) {
  const numLines = lines.length;
  return lines.reduce((result, entry, index) => {
    var separator = index > 2 ? " >>>> \n" : "\n";
    if (index == numLines - 1) {
      separator = "(end)\n";
    }
    return result + entry + separator;
  }, "");
}
console.log(constructOutput(convertToLines(data)));

The convertToLines uses the filter to dump empty entries which you get from the new line at the end of the string. And then we have to tack that on to the (end) separator`.

over 4 years ago · Santiago Trujillo Denunciar

0

To insert the >>>> you can use the regex: (?<=(.+\n.+){2,})(?=\n.)

  • The (?<=(.+\n.+){2,}) creates a positive lookbehind for three lines
  • The (?=\n.) creates a positive lookahead for a newline followed by any character

And then you can use the similar regex (?<=.)(?=\n*$) to add '(end)' to the last line.

const input = "Retract-A-Banner\n**Template:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n";

const output = input
                .replace(/(?<=(.+\n.+){2,})(?=\n.)/g, ' >>>> ')
                .replace(/(?<=.)(?=\n*$)/, '(end)');

console.log(input);
console.log(output);


If you are using a literal backslash followed by a 'n' in your string, it would be formatted as \\\n in your JS string literal.

In that case you can use the regex (?<=(.+\\n.+){2,})(?=\\n.) to insert the >>>> and the regex (?=\\n$) for the (end).

const input = "Retract-A-Banner\\n**Template:\\nFull Color\\nImprint Information:\\nCL: 488 Special Ops Wing\\n353 SOW Integrated Resilience Optimization Network\\n";

const output = input
                .replace(/(?<=(.+\\n.+){2,})(?=\\n.)/g, ' >>>> ')
                .replace(/(?=\\n$)/, '(end)');

console.log(input);
console.log(output);

over 4 years ago · Santiago Trujillo Denunciar

0

  • Convert string into an array of strings

     string.split(/\n/)
    
  • Then use .flatMap() and a chained ternary as callback. Each condition is based on index

      idx < 3 ? [str+' \n']
      :
      idx === arr.length -1 ? [str+' (end)'] 
      : 
      [str+' >>>> \n']
    
  • Then .join('') the array back into a string

let test = `Retract-A-Banner\nTemplate:\nFull Color\nImprint Information:\nCL: 488 Special Ops Wing\n353 SOW Integrated Resilience Optimization Network\n" Expected Output:"Retract-A-Banner\nTemplate:\nFull Color \nImprint Information: \nCL: 488 Special Ops Wing \n353 SOW Integrated Resilience Optimization Network`;

const formatLines = string => string.split(/\n/).flatMap((str, idx, arr) => idx < 3 ? [str + ' \n '] : idx === arr.length - 1 ? [str + ' (end) '] : [str + ' >>>> \n ']).join('');

console.log(formatLines(test));

over 4 years ago · Santiago Trujillo 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