如何从这种字符串中获取关键字

我需要从这种字符串中获取“付款类型和客户类型


   Examples:

      |    D           | A  | B | C |

      |      NZ       | AAA           |     BBB    |      NZ       |

      |      AZ       | CCC           |    DDD        |  AZ  |

      |      CA      | EEE            |    FFF        |  CA  |




我应该尝试获取模式并为此编写函数吗?或者我可以找到一些图书馆来检测它


因此输出应为{付款:[“ AAA”,“ CCC”,'EEE'],客户:[“ BBB”,'DDD“,” FFF“]}


function detect(str){

  let countBar=1

  let countBar2=0

  let paymentLoc=NaN

  let customerLoc=NaN

  let after =0

  let arr1=str.split(" ")

  arr1=arr1.filter(item=>{return item!==""})

  let newStr=''

  for(let i=0;i<arr1.length;i++){

    arr1[i].trim()

    if(arr1[i]==='|'){

      countBar++

    }

    if(arr1[i]==="||"){

      countBar2++

    }

    if(arr1[i].includes("payment")){

      paymentLoc=i

    }


   after=((countBar/(countBar2))-1)*2

let sol=[]

  for(let i=0;i<arr1.length;i++){

  if(arr1[i].includes("payment")){

    console.log('payment index',i)

      sol.push(arr1[i+after+1])

    }

    if(arr1[i].includes("customer")){

      console.log('customer index',i)

      sol.push(arr1[i+after+1])

    }



  }



  newStr=arr1.join('')

  console.log(newStr)



}


小怪兽爱吃肉
浏览 155回答 2
2回答

qq_花开花谢_0

这是一个试图用|分开的尝试。var text = `|&nbsp; &nbsp; market&nbsp; &nbsp; &nbsp;| payment type&nbsp; | customer type |&nbsp; translation&nbsp; |&nbsp; |&nbsp; &nbsp; &nbsp; NZ&nbsp; &nbsp; &nbsp; &nbsp;| AAA&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp;BBB&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; NZ&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; |&nbsp; &nbsp; &nbsp; AZ&nbsp; &nbsp; &nbsp; &nbsp;| CCC&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; DDD&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; AZ&nbsp; |&nbsp; |&nbsp; &nbsp; &nbsp; CA&nbsp; &nbsp; &nbsp; | EEE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp; FFF&nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; CA&nbsp; |`;var result = {&nbsp; &nbsp; payment: text.split('|').filter((f, i) => i-7 >= 0 && (i - 7) % 5 == 0)&nbsp; &nbsp; &nbsp; &nbsp;.map(p => p.trim()),&nbsp; &nbsp; customer: text.split('|').filter((f, i) => i-8 >= 0 && (i - 8) % 5 == 0)&nbsp; &nbsp; &nbsp; &nbsp;.map(p => p.trim())}console.log(result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript