文末附上 js 版本,需求简单概括为 去除一段文本中的不可见字符,要求汉字之间无空白,英文之间多个空白合并成一个
注 v1 和 v2 均为 typescript 代码
v1
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | 
 
 function deleteTrim(text: string): string {
 
 let txtTrim = text.replace(/\s{2,}/g, " ");
 
 txtTrim = txtTrim.replace(/[\r\n\t]/g, "");
 
 
 const strArr = txtTrim.split("");
 let lastChinese;
 const arrStr = [];
 strArr.forEach((char) => {
 if (char === " ") {
 if (!lastChinese) {
 arrStr.push(char);
 }
 } else {
 arrStr.push(char);
 }
 if (/.*[\u2E80-\u9FFF]+.*$/.test(char)) {
 
 lastChinese = true;
 } else {
 lastChinese = false;
 }
 });
 return arrStr.join("");
 }
 
 | 
v2
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | 
 
 
 
 
 
 
 
 
 
 function deleteTrim(text?: string): string | undefined {
 if (!text) {
 return undefined;
 }
 let lastChinese = false,
 lastBlank = false;
 const arrStr = [];
 Array.from(text.trim()).forEach((char) => {
 if (isBlank(char)) {
 if (!lastChinese && !lastBlank) {
 arrStr.push(" ");
 }
 lastBlank = true;
 lastChinese = false;
 } else {
 arrStr.push(char);
 lastBlank = false;
 if (/[\u2E80-\u9FFF]$/.test(char)) {
 
 lastChinese = true;
 } else {
 lastChinese = false;
 }
 }
 });
 return arrStr.join("");
 }
 
 
 
 
 
 
 function isBlank(s: string): boolean {
 if (s.length > 1) {
 throw Error("只能传递单字符");
 }
 switch (s) {
 case "\u0020":
 case "\u0008":
 case "\u0009":
 case "\u000A":
 case "\u000B":
 case "\u000C":
 case "\u000D":
 case "\u00A0":
 case "\u2028":
 case "\u2029":
 case "\uFEFF":
 case "\u200E":
 case "\u2006":
 case "\u200D":
 case "":
 return true;
 default:
 return false;
 }
 }
 
 | 
JavaScript 版本
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 
 | function deleteTrim(text) {if (!text) {
 return undefined;
 }
 let lastChinese = false,
 lastBlank = true;
 const arrStr = [];
 Array.from(text).forEach((char) => {
 if (isBlank(char)) {
 if (!lastChinese && !lastBlank) {
 arrStr.push(" ");
 }
 lastBlank = true;
 lastChinese = false;
 } else {
 arrStr.push(char);
 lastBlank = false;
 if (/[\u2E80-\u9FFF]$/.test(char)) {
 
 lastChinese = true;
 } else {
 lastChinese = false;
 }
 }
 });
 return arrStr.join("");
 }
 
 function isBlank(s) {
 if (s.length > 1) {
 throw Error("只能传递单字符");
 }
 switch (s) {
 case "\u0020":
 case "\u0008":
 case "\u0009":
 case "\u000A":
 case "\u000B":
 case "\u000C":
 case "\u000D":
 case "\u00A0":
 case "\u2028":
 case "\u2029":
 case "\uFEFF":
 case "\u200E":
 case "\u2006":
 case "\u200D":
 case "":
 return true;
 default:
 return false;
 }
 }
 
 |