|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Papa from 'papaparse'; |
| 3 | + |
| 4 | +function AuraRegexTool() { |
| 5 | + const [csvData, setCsvData] = useState([]); |
| 6 | + const [pattern, setPattern] = useState(''); |
| 7 | + const [flags, setFlags] = useState(''); |
| 8 | + const [text, setText] = useState(''); |
| 9 | + const [replacement, setReplacement] = useState(''); |
| 10 | + const [highlightedText, setHighlightedText] = useState(''); |
| 11 | + const [highlightedCSV, setHighlightedCSV] = useState([]); |
| 12 | + const [codeSnippet, setCodeSnippet] = useState(''); |
| 13 | + |
| 14 | + // Parse CSV |
| 15 | + const handleCSV = (e) => { |
| 16 | + const file = e.target.files[0]; |
| 17 | + Papa.parse(file, { |
| 18 | + header: true, |
| 19 | + skipEmptyLines: true, |
| 20 | + complete: (results) => setCsvData(results.data), |
| 21 | + }); |
| 22 | + }; |
| 23 | + |
| 24 | + // Update highlights and code snippet |
| 25 | + const updateHighlight = () => { |
| 26 | + try { |
| 27 | + const regex = new RegExp(pattern, flags); |
| 28 | + |
| 29 | + // Highlight text matches |
| 30 | + const textHtml = text.replace(regex, match => `<span class="highlight">${match}</span>`); |
| 31 | + setHighlightedText(textHtml); |
| 32 | + |
| 33 | + // Highlight CSV matches |
| 34 | + const csvHtml = csvData.map(row => |
| 35 | + Object.fromEntries( |
| 36 | + Object.entries(row).map(([key, value]) => [ |
| 37 | + key, |
| 38 | + value.replace(regex, match => `<span class="highlight">${match}</span>`) |
| 39 | + ]) |
| 40 | + ) |
| 41 | + ); |
| 42 | + setHighlightedCSV(csvHtml); |
| 43 | + |
| 44 | + // Update live code snippet |
| 45 | + const snippet = ` |
| 46 | +const regex = /${pattern}/${flags}; |
| 47 | +const matchesInText = text.match(regex); |
| 48 | +const matchesInCSV = csvData.map(row => |
| 49 | + Object.fromEntries( |
| 50 | + Object.entries(row).map(([key, value]) => [key, value.match(regex)]) |
| 51 | + ) |
| 52 | +); |
| 53 | + `; |
| 54 | + setCodeSnippet(snippet.trim()); |
| 55 | + } catch { |
| 56 | + setHighlightedText('<span class="error">Invalid regex</span>'); |
| 57 | + setHighlightedCSV([]); |
| 58 | + setCodeSnippet('// Invalid regex'); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // Apply replacements |
| 63 | + const applyReplacement = () => { |
| 64 | + try { |
| 65 | + const regex = new RegExp(pattern, flags); |
| 66 | + const replacedText = text.replace(regex, replacement); |
| 67 | + const replacedCSV = csvData.map(row => |
| 68 | + Object.fromEntries( |
| 69 | + Object.entries(row).map(([key, value]) => [key, value.replace(regex, replacement)]) |
| 70 | + ) |
| 71 | + ); |
| 72 | + setText(replacedText); |
| 73 | + setCsvData(replacedCSV); |
| 74 | + updateHighlight(); |
| 75 | + } catch { |
| 76 | + setHighlightedText('<span class="error">Invalid regex</span>'); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + // Download CSV |
| 81 | + const downloadCSV = () => { |
| 82 | + const csvString = Papa.unparse(csvData); |
| 83 | + const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' }); |
| 84 | + const link = document.createElement("a"); |
| 85 | + link.href = URL.createObjectURL(blob); |
| 86 | + link.download = "processed.csv"; |
| 87 | + link.click(); |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div style={{ fontFamily: 'monospace', padding: '20px' }}> |
| 92 | + <h2>Aura Universal Regex Tool</h2> |
| 93 | + |
| 94 | + <input type="file" accept=".csv" onChange={handleCSV} /><br /><br /> |
| 95 | + <input |
| 96 | + type="text" |
| 97 | + placeholder="Regex pattern" |
| 98 | + value={pattern} |
| 99 | + onChange={e => { setPattern(e.target.value); updateHighlight(); }} |
| 100 | + /> |
| 101 | + <input |
| 102 | + type="text" |
| 103 | + placeholder="Flags (g,i,m)" |
| 104 | + value={flags} |
| 105 | + onChange={e => { setFlags(e.target.value); updateHighlight(); }} |
| 106 | + style={{ width: '80px', marginLeft: '10px' }} |
| 107 | + /><br /><br /> |
| 108 | + |
| 109 | + <textarea |
| 110 | + placeholder="Test text..." |
| 111 | + value={text} |
| 112 | + onChange={e => { setText(e.target.value); updateHighlight(); }} |
| 113 | + rows={5} |
| 114 | + cols={60} |
| 115 | + /><br /><br /> |
| 116 | + |
| 117 | + <input |
| 118 | + type="text" |
| 119 | + placeholder="Replacement (optional)" |
| 120 | + value={replacement} |
| 121 | + onChange={e => setReplacement(e.target.value)} |
| 122 | + /> |
| 123 | + <button onClick={applyReplacement}>Apply Replacement</button> |
| 124 | + <button onClick={downloadCSV} style={{ marginLeft: '10px' }}>Download CSV</button> |
| 125 | + |
| 126 | + <h3>Highlighted Text:</h3> |
| 127 | + <pre dangerouslySetInnerHTML={{ __html: highlightedText }} /> |
| 128 | + |
| 129 | + {csvData.length > 0 && ( |
| 130 | + <> |
| 131 | + <h3>Highlighted CSV:</h3> |
| 132 | + <table border="1" cellPadding="5"> |
| 133 | + <thead> |
| 134 | + <tr> |
| 135 | + {Object.keys(csvData[0]).map((col, i) => <th key={i}>{col}</th>)} |
| 136 | + </tr> |
| 137 | + </thead> |
| 138 | + <tbody> |
| 139 | + {highlightedCSV.map((row, i) => ( |
| 140 | + <tr key={i}> |
| 141 | + {Object.entries(row).map(([key, value], j) => ( |
| 142 | + <td key={j} dangerouslySetInnerHTML={{ __html: value }} /> |
| 143 | + ))} |
| 144 | + </tr> |
| 145 | + ))} |
| 146 | + </tbody> |
| 147 | + </table> |
| 148 | + </> |
| 149 | + )} |
| 150 | + |
| 151 | + <h3>Live Regex Code Snippet:</h3> |
| 152 | + <pre>{codeSnippet}</pre> |
| 153 | + |
| 154 | + <h3>Regex Cheat Sheet:</h3> |
| 155 | + <ul> |
| 156 | + <li>Digits: <code>\d</code></li> |
| 157 | + <li>Word chars: <code>\w</code></li> |
| 158 | + <li>Whitespace: <code>\s</code></li> |
| 159 | + <li>Email: <code>^[\w.-]+@[a-zA-Z\d.-]+\.[a-zA-Z]{2,}$</code></li> |
| 160 | + <li>Phone: <code>^\+?\d{10,15}$</code></li> |
| 161 | + <li>Date YYYY-MM-DD: <code>^\d{4}-\d{2}-\d{2}$</code></li> |
| 162 | + </ul> |
| 163 | + </div> |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +export default AuraRegexTool; |
0 commit comments