【JS】消除头尾的换行符、空格符

devtools/2024/10/15 17:25:37/
htmledit_views">

一、需求说明

删除 ckeditor 生成的文本的开头和结尾的额外换行符、空格符,但不删除文本本身之间的空格、换行内容。

举例:

1、原始数据

2、期望数据

二、需求分析

1. 从 ckeditor 获得的全部字符串解析content.getData()为 HTMLCollection。

2. 遍历 html 标签并通过检查属性textContent删除任何没有文字的标签,一旦标签有文字退出循环。

3. 在有内容的标签内循环,通过拆分将字符串转换为数组str.split(' '),遍历数组并删除每个段落头尾的<br>或者&nbsp;直到到达这些标签和实体以外的任何内容,然后退出循环。

三、解决办法

1、代码

html" title=javascript>javascript">/*** @description: 去除段落首位的空白符和空白行* @param {number}* @return {*}*/
const trimedCkEditorText = ckEditor => {// let contentStr = ckEditor.getData();// 传入的数据为富文本时let contentStr = ckEditor;// 传入的数据为html字符串时// Remove Extra whitespaces at the begining of the textcontentStr = trimedCkEditorTextAt(contentStr, true);// Remove Extra whitespaces at the end of the textcontentStr = trimedCkEditorTextAt(contentStr, false);return contentStr;
};/*** @description: 去除段首尾的空白行 + 去除段首尾的空白符* @param {number}* @return {*}*/
const trimedCkEditorTextAt = (contentStr, startOfText) => {const parser = new DOMParser();const doc = parser.parseFromString(contentStr, "text/html");// Check first childwhile (doc.body.children.length) {const index = startOfText ? 0 : doc.body.children.length - 1;const child = doc.body.children[index];if (child.textContent.replace(/\s/g, "").length) {// Remove <br> tags:清除首尾空白行while (child.children.length) {const index = startOfText ? 0 : child.children.length - 1;const grandechild = child.children[index];if (grandechild.localName === "br") grandechild.remove();else break;}// Remove &nbsp; tags:清除首尾空白符const childTextArray = child.innerHTML.split(" ");while (childTextArray.length) {const index = startOfText ? 0 : childTextArray.length - 1;// console.log(childTextArray, "-------", childTextArray.length, index);if (childTextArray[index] === "&nbsp;" || childTextArray[index] === "&nbsp;&nbsp;") {childTextArray.splice(index, 1); // 删除尾部的空格:全是空格的情况// console.log(childTextArray, "1111", childTextArray.length, index);} else if ((index !== 0 || (index === 0 && childTextArray.length === 1)) &&childTextArray[index] !== "&nbsp;" &&childTextArray[index] !== "&nbsp;&nbsp;" &&childTextArray[index].lastIndexOf("&nbsp;") + 6 === childTextArray[index].length) {childTextArray[index] = childTextArray[index].replace(/\&nbsp;$/, ""); // 删除尾部的空格:“文字+&nbsp;”的情况// console.log(childTextArray, "2222", childTextArray.length, index);} else if (index === 0 &&childTextArray[index] !== "&nbsp;" &&childTextArray[index] !== "&nbsp;&nbsp;" &&childTextArray[index].indexOf("&nbsp;") === 0) {childTextArray[index] = childTextArray[index].replace(/^(&nbsp;|)+/g, ""); // 删除头部的空格:“&nbsp;+文字”的情况// console.log(childTextArray, "3333", childTextArray.length, index);} else break;}child.innerHTML = childTextArray.join(" ");break;} else {child.remove();}}return doc.body.innerHTML;
};

2、举例

(1)输入的数据

(2)处理过程打印

(3)处理后得到的数据

3、其他说明

如果文本数据为普通字符串,则可以使用一下正则表达式去除文本首尾的空白符和换行符

html" title=javascript>javascript">"字符串".replace(/^\s+|\s+$/g, ""), // 去除首尾的空白符、换行符

 四、参考链接

参考1:

CKeditor 删除开头多余的空格 - 修剪_慕课猿问

参考2:

html" title=javascript>javascript 去除字符串首尾空格_js 去除掉首尾所有的空格-CSDN博客


http://www.ppmy.cn/devtools/126264.html

相关文章

【K8S系列】Kubernetes 集群中的网络常见面试题

在 Kubernetes 面试中,网络是一个重要的主题。理解 Kubernetes 网络模型、服务发现、网络策略等概念对候选人来说至关重要。以下是一些常见的 Kubernetes 网络面试题及其答案,帮助你准备面试。 1. Kubernetes 的网络模型是什么样的? 问题: Kubernetes 的网络模型是怎样的?…

Java数据结构练习题

关于数据结构的题目(会更新) 一、顺序表 二、链表 1.链表分割 链接 import java.util.*;/* public class ListNode {int val;ListNode next null;ListNode(int val) {this.val val;} }*/ //思路&#xff1a; //1.分为两个链表,一个链表放小于x的节点&#xff0c;一个链表…

校车购票微信小程序的设计与实现(lw+演示+源码+运行)

摘 要 由于APP软件在开发以及运营上面所需成本较高&#xff0c;而用户手机需要安装各种APP软件&#xff0c;因此占用用户过多的手机存储空间&#xff0c;导致用户手机运行缓慢&#xff0c;体验度比较差&#xff0c;进而导致用户会卸载非必要的APP&#xff0c;倒逼管理者必须改…

交通路口智能监测平台实现

本文所涉及所有资源均在传知代码平台可获取。 目录 1.概述 2.工程文件简介 2.1 工程文件结构 2.2 训练检测模型 2.2.1 准备数据集 2.2.2 训练自己的权重文件 2.2.3 使用自己的权重文件 3.系统可视化 3.1 打开摄像头 3.2 上传视频监测 3.3 统计结果显示 4.效果 5.总结 6.训练环境…

LeetCode50.Pow(x, n)

题目要求 实现 pow(x, n) &#xff0c;即计算 x 的整数 n 次幂函数&#xff08;即x的n次方&#xff09;。 破题思路 快速幂算法 求x的n次方最简单的方法是写一个n次的循环&#xff0c;每一次循环乘以一个x&#xff0c;那这样的话时间复杂度就是n。 快速幂法可以将时间复杂度…

门店收银系统源码-php+flutter+uniapp

1. 系统开发语言 核心开发语言: PHP、HTML5、Dart 后台接口: PHP7.3 后台管理网站: HTML5vue2.0element-uicssjs 线下收银台&#xff08;安卓/PC收银、安卓自助收银&#xff09;: Dart3 框架&#xff1a;Flutter 3.19.6 移动店务助手: uniapp 线上商城: uniapp 2.线下收…

鸿蒙Swiper动态加载翻页数据(等同于安卓动态加载viewPager)

我这里是加载一个实体类列表 类似 List 的数据&#xff0c;那么首先写一个dataSource&#xff1a; export class MyDataSource implements IDataSource {private list: MyBean[] []constructor(list: MyBean[]) {this.list list}totalCount(): number {return this.list.len…

用SQLyog连接mysql提示2058错误

1)在cmd下(必须是这个,不能是gitbash) // step1:修改下数据库 C:\Users\elex>mysql -uroot -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 97 Server version: 8.1.0 MySQL Community Server - GPLCopy…