Browse Source

标点规则:中间连续标点保留第一个,开头去掉,结尾保留第一个,灰色不显示

zhouna 6 years ago
parent
commit
0ab03d2eba
1 changed files with 12 additions and 31 deletions
  1. 12 31
      src/utils/tools.js

+ 12 - 31
src/utils/tools.js

@@ -489,37 +489,18 @@ function filterArr(arr){
 
 function filterDataArr(arrTmp){   //数据处理
     let tmpArr = [];
-    let arr = arrTmp.join('').split('');
-    for(let i = 0;i < arr.length;i++){
-        if(!(arr[i].match(config.punctuationReg)||arr[i-1])){        //只有标点符号或者前一个标签无值是(说明本标点灰显,不显示在预览中)
-            arr.splice(i,1,'');
-        }
-        if(arr[i] && arr[i].indexOf('undefined') == -1){
-            if(tmpArr[tmpArr.length-1] != ',' && tmpArr[tmpArr.length-1] != ',' ){
-                tmpArr.push(arr[i])
-            }else if((tmpArr[tmpArr.length-1] == ',' && (arr[i] != ',' || arr[i] != ',')) || (tmpArr[tmpArr.length-1] == ',' && (arr[i] != ',' || arr[i] != ','))) {
-              if(arr[i] == '。'){     //前面逗号后面句号 [',','。']
-                tmpArr.pop();
-                tmpArr.push(arr[i]);
-              }else if(tmpArr[tmpArr.length-2] == '。' && (tmpArr[tmpArr.length-1] == ',' || tmpArr[tmpArr.length-1] == ',')){//前面句号后面逗号 ['。',',']
-                tmpArr.pop();
-                tmpArr.push(arr[i]);
-              }else if(arr[i] == ',' || arr[i] == ','){    //  中英文逗号交替[',',',']
-                tmpArr.pop();
-                tmpArr.push(arr[i]);
-              }else if(arr[i] == '、'){    //前面逗号后面句号 [',','、']
-                tmpArr.pop();
-                tmpArr.push(arr[i]);
-              }else if(tmpArr[tmpArr.length-2] == '、' && (tmpArr[tmpArr.length-1] == ',' || tmpArr[tmpArr.length-1] == ',')){    //前面逗号后面句号 ['、',',']
-                tmpArr.splice(tmpArr.length-2,1);
-                tmpArr.push(arr[i]);
-              }else{
-                tmpArr.push(arr[i]);
-              }
-            }
-        }
-    }
-    return (tmpArr.join('')).replace(/^,+/,"").replace(/,+$/,"").replace(/^,+$/,"").replace(/,+$/,"").replace(/^。+/,"").replace(/。+$/,"。");
+    tmpArr = arrTmp.map((it,i)=>{     //连续的标点符号保留第一个
+      if(!it.match(config.punctuationReg)&&!arrTmp[i-1]){        //只有标点符号或者前一个标签无值是(说明本标点灰显,不显示在预览中)
+          return '';
+      }
+      return it.replace(config.punReg,function(word){
+        return word.substr(0,1);
+      });
+    });
+
+    return tmpArr.join('').replace(/^[,,.。;;、]+/,'').replace(/[,,.。;;、]+$/,function(word){
+        return word.substr(0,1);
+    });      //去掉开头的标点符号,最后的标点保留第一个
 }
 // 取消默认行为
 function preventDefault(event) {