猿问

CKeditor 删除开头多余的空格 - 修剪

我试图删除ckeditor 生成的文本开头结尾的额外空格,但不删除文本本身之间的空格。

我在开始时有很多空间,然后是实际文本。

所以我正在尝试为 ckeditor 生成的文本编写一个修剪函数,传统的 trim() 不能像文本包含的那样工作<p></p> &nbsp;,有时 <br>所以我不能只写一个正则表达式。

生成的文本示例

<p><br><br><br><br><br><br><br><br><br><br><br>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><br><br><br><br><br><br>here text begins</p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>another line</p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>third line </p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><strong>fourth line</strong></p><p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><span class="text-huge"><strong>sdsdsdsdsd</strong></span></p><h2><span class="text-huge"><i><strong><u>sdsdsdsdsdsdsd</u></strong></i></span><br><br><br><br><br><br><span class="text-huge"><i><strong><u>csdsdsdsdsdsds</u></strong></i></span><br><br><br><br><br><br><br><br><br>&nbsp;</h2>

我想删除所有标签,直到这里的文本开始,但这个文本可能会有所不同,可能是任何东西,

有什么建议么?


回首忆惘然
浏览 222回答 1
1回答

吃鸡游戏

好的,我已经设法通过解决方法解决了这个问题,想法是:1- 将我们从 ckeditor 获得的全部字符串解析content.getData()为 HTMLCollection,2- 遍历 html 标签并通过检查属性textContent删除任何没有文字的标签,一旦标签有文字退出循环。3-在其中包含单词的标签内循环,并通过拆分将字符串转换为单词数组str.split(' ')并遍历数组并删除每个单词,<br>或者&nbsp;直到到达这些标签和实体以外的任何内容,然后退出循环。4- 您需要从(从 ckeditor 获得的全部文本)的开头和结尾经历这个过程。&nbsp; &nbsp; &nbsp;trimedCkEditorText() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let contentStr = this.content.getData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove Extra whitespaces at the begining of the text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentStr = this.trimedCkEditorTextAt(contentStr, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove Extra whitespaces at the end of the text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentStr = this.trimedCkEditorTextAt(contentStr, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return contentStr;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; trimedCkEditorTextAt(contentStr, startOfText) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const parser = new DOMParser();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const doc = parser.parseFromString(contentStr, "text/html")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check first child&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(doc.body.children.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const index = startOfText ? 0 : doc.body.children.length - 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const child = doc.body.children[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(child.textContent.replace(/\s/g, '').length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove <br> tags&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(child.children.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const index = startOfText ? 0 : child.children.length - 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const grandechild = child.children[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(grandechild.localName === 'br') grandechild.remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const childTextArray = child.innerHTML.split(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(childTextArray.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const index = startOfText ? 0 : childTextArray.length - 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(childTextArray[index] === '&nbsp;') childTextArray.splice(index, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.innerHTML = childTextArray.join(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return doc.body.innerHTML;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答