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"
Split by \n and recreate your string with the conditions:
(end)>>>> \n\n will be fineEDIT:
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);
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.
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`.
To insert the >>>> you can use the regex: (?<=(.+\n.+){2,})(?=\n.)
(?<=(.+\n.+){2,}) creates a positive lookbehind for three lines(?=\n.) creates a positive lookahead for a newline followed by any characterAnd 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);
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));