In javascript, I am trying to write a regex that will remove all spaces between occurrences of \n.
For example, the string Test\n \n \n \n\n 1234 would turn into Test\n\n\n\n\n 1234.
How do I do this?
You can use non-greedy matching of whitespace \s*? bracketed by lookbehind and lookahead assertions for \n, (expressed as (?<=\n) and (?=\n) respectively) and replace it with an empty string.
const input = "Test\n \n \n \n\n 1234";
const result = input.replace(/(?<=\n)\s*?(?=\n)/g, '');
console.log(JSON.stringify(result));
let t = "test\n\n \n\n\n \n"
console.log(JSON.stringify(t.replaceAll(/(?<=\n)\s*?(?=\n)/g, '')))
.trim()?.replaceAll()^([\s\t]*)\n$with "\n" would work? it should match every single line replacing the space part before the newline with nothing but a newline.\n +would work.. just replace with new line.