>

다음과 같은 예가 있습니다 : https://regexr.com/49sdt 보시다시피 11 개의 요소가 있습니다. 이제 항목 목록 만 포함하는 항목을 분할하고 싶습니다. [Sample .... sample2]

여기에서 내 코드를 확인할 수 있습니다 : https://repl.it/repls/PriceyNecessaryProfiles 보시다시피 23 개의 요소가 있습니다.

예상 한 결과는 다음과 같습니다 :

[ 'Sample\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample2\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample3\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample4\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample5\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample6\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample7\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample8\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample9\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample10\n•\tThis is sample 1\n•\tThis is sample 2',
  'Sample11\n•\tThis is sample 1\n•\tThis is sample 2' ]

어떻게 해결할 수 있습니까?


  • 답변 # 1

    /\n*\d+\.\s*/g 에서 문자열을 나눌 수 있습니다   samples 를 얻기 위해  개별 배열 항목에. slice 사용  배열의 시작과 끝에서 빈 문자열을 제거합니다.

    const rawData = `1. Sample
    •	This is sample 1
    •	This is sample 2
    2. Sample2
    •	This is sample 1
    •	This is sample 2
    3. Sample3
    •	This is sample 1
    •	This is sample 2
    4. Sample4
    •	This is sample 1
    •	This is sample 2
    
    5. Sample5
    •	This is sample 1
    •	This is sample 2
    6. Sample6
    •	This is sample 1
    •	This is sample 2
    7. Sample7
    •	This is sample 1
    •	This is sample 2
    8. Sample8
    •	This is sample 1
    •	This is sample 2
    9. Sample9
    •	This is sample 1
    •	This is sample 2
    10. Sample10
    •	This is sample 1
    •	This is sample 2
    11. Sample11
    •	This is sample 1
    •	This is sample 2
    `
    let output = rawData.split(/\n*\d+\.\s*|\n$/g).slice(1, -1)
    console.log(output)
    
    

관련 자료

  • 이전 python - 팬더에 대한 Numpy 요구 사항이 충족되지 않음
  • 다음 반응 네이티브의 구성 요소 간 데이터 전송