Regex - Less results than expected (Javascript)

Describe the problem/error/question

A javascript extracting table ligne from a text_result. The javascript includes a regex which working but not display all results from the text_result.

What is the error message (if any)?

no

Please share your workflow

{

  • “nodes”: [*
  • {*
  •  "parameters": {*
    
  •    "jsCode": "const text = $input.first().json.text_a_traiter; // Supposons que le texte est dans item.json.text\n\n// Normaliser les espaces et les décimales\nconst normalizedText = text\n  //.replace(/\\s+/g, ' ') // Remplacer plusieurs espaces par un seul espace\n  .replace(/(\\d)\\.(\\d)/g, '$1,$2'); // Remplacer les points par des virgules pour les décimales\n\nconst lines = normalizedText.split('\\n');\nconst result = [];\n\nlines.forEach(line => {\n  if (line.startsWith('Code Affaire') || line.trim() === '') return; // Ignorer l'en-tête et les lignes vides\n\n  // Expression régulière plus flexible\n  const regex = /(\\d{3}\\s\\d{3}\\s\\d{3}\\s\\d{3})\\s+(.+?)\\s+(\\d{2}\\/\\d{2}\\/\\d{4})\\s+(\\d+,\\d+)\\s+([\\d,]+\\s[€$£])\\s+([\\d,]+\\s[€$£])\\s+(\\d+,\\d+%)\\s+([\\d,]+\\s[€$£])/;\n\n  const match = line.match(regex);\n  if (match) {\n    result.push({\n      json: {\n        code_affaire: match[1],\n        designation: match[2],\n        commande: match[3], // Date de commande\n        unite: match[4],   // Unité\n        tarif: match[5],   // Tarif\n        montant_ht: match[6], // Montant HT\n        tva: match[7],     // TVA\n        montant_ttc: match[8] // Montant TTC\n      }\n    });\n  } else {\n    // Log pour déboguer les lignes non correspondantes\n    console.log(`Ligne non correspondante: ${line}`);\n  }\n});\n\nreturn result;\n"*
    
  •  },*
    
  •  "type": "n8n-nodes-base.code",*
    
  •  "typeVersion": 2,*
    
  •  "position": [*
    
  •    -1020,*
    
  •    260*
    
  •  ],*
    
  •  "id": "0c77f722-ce9c-4a46-96c4-274af9d19afa",*
    
  •  "name": "Code operationnel mais manque des lignes"*
    
  • }*
  • ],*
  • “connections”: {},*
  • “pinData”: {},*
  • “meta”: {*
  • “templateCredsSetupCompleted”: true,*
  • “instanceId”: “a64a2237cad0409b2db1149dc794b1ab25f6e3dc62348c42982b3a1f85496117”*
  • }*
    }
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Output

Input
$input.first().json.text_a_traiter

janvier 2024\n54 \n31/01/2024\nMontant HT TVA Montant TTC\nTotal TVA 5,50% \n9 799,13 € \n5,50% \n10 338,09 €\nTotal TVA 20,00% \n10 973,38 € \n20,00% \n13 168,07 €\nTotal 20 772,51 € 23 506,16 €\nCode Affaire Désignation Commande Unité Tarif Montant HT TVA Montant TTC\n117 253 668 000 Pack Colorie et Décore LOL (M01172-1) 31/01/2024 668,000 2,496 € 1 667,33 € 20,00% 2 000,80 €\n212 153 668 000 Coffret créatif LOL XS (M02121-1) 31/01/2024 265,000 3,329 € 882,19 € 20,00% 1 058,63 €\n438 653 668 000 Pochette dinosaures (M04386-1) 31/01/2024 225,000 2,079 € 467,78 € 20,00% 561,34 €\n499 253 668 000 Pochette activités Girly (M04992-1) 31/01/2024 1 028,000 2,839 € 2 918,49 € 5,50% 3 079,01 €\n499 753 668 000 Mon carnet fantaisie (M04997-1) 31/01/2024 1 459,000 4,716 € 6 880,64 € 5,50% 7 259,08 €\n503 853 668 000 Coffret créatif Licorne XS (M05038-1) 31/01/2024 991,000 3,329 € 3 299,04 € 20,00% 3 958,85 €\n504 153 664 000 Coffret créatif Licorne Emoji (M05041-1) 31/01/2024 434,000 4,162 € 1 806,31 € 20,00% 2 167,57 €\n508 253 668 000 Coffret créatif LOL XL (M05082-1) 31/01/2024 524,000 4,162 € 2 180,89 € 20,00% 2 617,07 €\n528 353 668 000 Set papeterie LOL (M05283-1) 31/01/2024 99,000 1,663 € 164,64 € 20,00% 197,57 €\n528 653 668 000 Stylos gel LOL (M05286-1) 31/01/2024 243,000 2,079 € 505,20 € 20,00% 606,24 €\nFACTURES\nRelevé de vente à facturer\nPage 1 de 1

Information on your n8n setup

  • n8n version: 1.91.2 - Selh Hosted
  • Database (default: SQLite): no
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu 22.04

I don’t think this is n8n’s issue. Your regex and sample input string only yield 3 results. Try to work out your regex in an isolated regex tester/analyzer and then put it in the n8n code block when you have it working correctly.

2 Likes

Hi,

Thanks for your advice. I did it and optimized my regex

Now, it’s working zell

Tks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.