字符串分割

提取字符串中的單引號字符串做爲獨立個體,分割其他的以空格分號逗號的串javascript

let splitString = function(res){
      let reg = /\'(.*?)\'/ig;
      let maArr = res.match(reg);
      let optionArr = [];
      let reArr = [];
      let start = 0;
      let defineStr = '';
        maArr.map((key, index) => {
          let rekey = key.replace(/\ +/g, "@");
          let item = {};
          let keyindex = res.indexOf(key);
          reArr.push(rekey);
          item.start = start;
          item.end = keyindex;
          item.length = key.length;
          optionArr.push(item);
          start = keyindex + key.length;
        });
        optionArr.map((key,index) => {
          defineStr += (res.substring(key.start, key.end) + reArr[index]);
          if (index === (optionArr.length - 1)){
            defineStr +=res.substring(key.end + key.length, res.length)
          }
        })
        let spitStr = defineStr.replace(/,/g, " ").replace(/;/g, " ");
        const result = spitStr.split(" ").map(key => key.replace(/@/g, " "));
        return result;
      } 

複製代碼
相關文章
相關標籤/搜索