Note ... since the OP did mention ... "for server-end" ... the OP most probably needs to find a package which comes close to a browser's native DOMParser
Web API.
One approach was to use a mix of a parsed markup's (via DOMParser.parseFromString) innerText
string value and a multiline capturing regex like e.g. /^(?:\\n|\s)*(?<content>.*)/gm
together with matchAll
and an additional map
/ filter
task.
const htmlMarkup =
`<p>save</p>
<p>11<br />\nabc<br />\nabc<br />\nhello</p>\n\n<p>dfcs dcsd</p>\n\n<p>sdcsd<br />\nsdcsdc<br />\nsdcd</p>\n
<p>1</p>\n\n<p>11<br />\n111</p>\n\n<p>1111<br />\n11111</p>\n\n<p>1</p>\n\n<p> </p>\n`;
// see ... [https://regex101.com/r/4bzz4m/1]
const regXLineContent = /^(?:\\n|\s)*(?<content>.*)/gm;
const doc = (new DOMParser)
.parseFromString(htmlMarkup, "text/html");
console.log(
'... inner text ...',
doc
.body
.innerText
);
console.log(
'... list of pure content ...',
Array
.from(
doc
.body
.innerText
.matchAll(regXLineContent)
)
.map(match => match.groups.content)
.filter(content => content !== '')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Another, preferred approach, was to use a content split
ting regex like ... /\n(?:\\n|\s)*/g
... for directly getting an array of (valid) line contents.
Yet, as for the provided markup sample, and like with the former approach, one still needs to run the sanitizing filter
task.
const htmlMarkup =
`<p>save</p>
<p>11<br />\nabc<br />\nabc<br />\nhello</p>\n\n<p>dfcs dcsd</p>\n\n<p>sdcsd<br />\nsdcsdc<br />\nsdcd</p>\n
<p>1</p>\n\n<p>11<br />\n111</p>\n\n<p>1111<br />\n11111</p>\n\n<p>1</p>\n\n<p> </p>\n`;
// see ... [https://regex101.com/r/4bzz4m/2]
const regXLineSeparators = /\n(?:\\n|\s)*/g;
const doc = (new DOMParser)
.parseFromString(htmlMarkup, "text/html");
console.log(
'... inner text ...',
doc
.body
.innerText
);
console.log(
'... list of splitted content ...',
doc
.body
.innerText
.split(regXLineSeparators)
);
console.log(
'... list of pure content ...',
doc
.body
.innerText
.split(regXLineSeparators)
.filter(content => content !== '')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }