https://www.fwait.com/how-to-remove-empty-lines-in-javascript/

添加数据的时候使用js进行判断,删除空白行,并继续执行非空白的内容

let btnRemove = document.querySelector("button");
let output = document.querySelector("h1");
let myString = `
Line 1
Line 2
Line 3
`;
output.innerText = myString;
btnRemove.addEventListener("click", () => {
let regex = /^\s*$(?:\r\n?|\n)/gm;
let result = myString.replace(regex, "");
output.innerText = result;
});

使用.replace方法就可以进行替换,然后传入参数把空白行进行删除.replace(/^\s*$(?:\r\n?|\n)/gm, "")

https://itecnote.com/tecnote/javascript-regex-to-remove-white-spaces-blank-lines-and-final-line-break-in-javascript/