我正在使用 ngx-quill,输入正文返回一些 HTML 元素。
示例
<p><strong><em><u>"Soft fingers began to tap the sill of the car window, and the hard fingers tightened on the restless drawing sticks. In the doorways of the sun-beaten tenant houses, women sighed and then shifted feet so that the one that had been down was now on top, and the toes working. Dogs came sniffing near the owner cars and wetted on all four tires one after another. And chickens lay in the sunny dust and fluffed their feathers </u></em></strong></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><strong><em>to get the cleansing dust
我想删除除换行段落之外的所有 HTML 标记。
当帖子有多行/中断时,ngx-quill
添加了几个链接<p></p><p></p>
(见上文)
我尝试使用 replace
函数来剥离元素,但某些元素如 <u>
没有被删除。另外,如何将具有多个换行符的部分合并为一个换行符
我已经尝试过
post = '<p><strong><em><u>"Soft fingers began to tap the sill of the car window, and the hard fingers tightened on the restless drawing sticks. In the doorways of the sun-beaten tenant houses, women sighed and then shifted feet so that the one that had been down was now on top, and the toes working. Dogs came sniffing near the owner cars and wetted on all four tires one after another. And chickens lay in the sunny dust and fluffed their feathers </u></em></strong></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><strong><em>to get the cleansing dust down to the skin. In the little sties the pigs grunted inquiringly over the muddy remnants of the slops.""Soft fingers began to tap the sill of the car window, and the hard fingers tightened on the restless drawing sticks. In the doorways of the sun-beaten tenant houses, women sighed and then shifted feet so that the one that had been down was now on top, and the toes working. Dogs came sniffing near the owner cars and wetted on all four tires one after another. And chickens lay in the sunny dust and fluffed their feathers to get the cleansing dust down to the skin. In the little sties the pigs grunted inquiringly over the muddy remnants of the slops."</em></strong></p>'
function stripElements(post: any) {
let newPost = post;
newPost = newPost.replace('<u>', '<span>');
newPost = newPost.replace('</u>', '</span>');
newPost = post.replace('<strong>','');
newPost = newPost.replace('</strong>', '');
newPost = newPost.replace('<em>', '');
newPost = newPost.replace('</em>', '');
newPost = newPost.replace('<p><br></p>', '<p></p>')
return newPost;
}
最佳答案
您可以使用DOMParser API解析和操作 HTML 代码:
post = '<p><strong><em><u>"Soft fingers began to tap the sill of the car window, and the hard fingers tightened on the restless drawing sticks. In the doorways of the sun-beaten tenant houses, women sighed and then shifted feet so that the one that had been down was now on top, and the toes working. Dogs came sniffing near the owner cars and wetted on all four tires one after another. And chickens lay in the sunny dust and fluffed their feathers </u></em></strong></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><strong><em>to get the cleansing dust down to the skin. In the little sties the pigs grunted inquiringly over the muddy remnants of the slops.""Soft fingers began to tap the sill of the car window, and the hard fingers tightened on the restless drawing sticks. In the doorways of the sun-beaten tenant houses, women sighed and then shifted feet so that the one that had been down was now on top, and the toes working. Dogs came sniffing near the owner cars and wetted on all four tires one after another. And chickens lay in the sunny dust and fluffed their feathers to get the cleansing dust down to the skin. In the little sties the pigs grunted inquiringly over the muddy remnants of the slops."</em></strong></p>'
function stripElements(post) {
const doc = new DOMParser().parseFromString(post, 'text/html');
doc.querySelectorAll('body :not(p)').forEach(el => el.replaceWith(el.textContent))
return doc.body.innerHTML;
}
console.log(stripElements(post))
关于javascript - 从字符串中删除某些 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70726683/