dev-tools / regex.html
natep2234's picture
Upload regex.html with huggingface_hub
22a748c verified
Raw History Blame Contribute Delete
7.02 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>regex.test — Live regex tester with explanations</title>
<meta name="description" content="Test regular expressions in real-time with match highlighting and plain English explanations. Free, fast, no signup.">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #0d1117; color: #c9d1d9; font-family: -apple-system, 'Segoe UI', monospace; min-height: 100vh; padding: 3rem 1rem; display: flex; flex-direction: column; align-items: center; }
h1 { font-size: 2rem; color: #f0f6fc; margin-bottom: 0.25rem; }
.sub { color: #484f58; margin-bottom: 2rem; }
.panel { width: 100%; max-width: 700px; }
label { color: #8b949e; font-size: 0.85rem; display: block; margin-bottom: 0.35rem; margin-top: 1rem; }
.regex-row { display: flex; gap: 0.5rem; align-items: center; }
input, textarea { background: #161b22; border: 1px solid #30363d; border-radius: 6px; padding: 0.75rem 1rem; color: #f0f6fc; font-family: 'Courier New', monospace; font-size: 1rem; outline: none; width: 100%; }
input:focus, textarea:focus { border-color: #58a6ff; }
.flags { display: flex; gap: 0.75rem; margin-top: 0.5rem; }
.flags label { display: flex; align-items: center; gap: 0.3rem; cursor: pointer; color: #8b949e; margin-top: 0; }
.flags input[type=checkbox] { accent-color: #58a6ff; }
textarea { min-height: 120px; resize: vertical; line-height: 1.6; }
.result { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 1.25rem; margin-top: 1rem; line-height: 1.8; font-size: 0.95rem; }
.match-text { font-family: 'Courier New', monospace; line-height: 2; word-break: break-all; }
.match-text mark { background: rgba(88,166,255,0.25); color: #58a6ff; border-radius: 2px; padding: 0.1rem 0.2rem; }
.stats { color: #8b949e; font-size: 0.85rem; margin-top: 0.75rem; }
.stats span { color: #3fb950; }
.explain { color: #d2a8ff; margin-top: 0.75rem; font-size: 0.9rem; }
.error { color: #f85149; }
.footer { margin-top: 3rem; color: #30363d; font-size: 0.75rem; }
</style>
</head>
<body>
<h1>regex.test</h1>
<p class="sub">Live matching with explanations.</p>
<div class="panel">
<label>Pattern</label>
<div class="regex-row">
<span style="color:#484f58">/</span>
<input id="pattern" type="text" placeholder="[a-z]+@[a-z]+\.[a-z]{2,}">
<span style="color:#484f58">/</span>
</div>
<div class="flags">
<label><input type="checkbox" id="fg" checked> g (global)</label>
<label><input type="checkbox" id="fi"> i (case-insensitive)</label>
<label><input type="checkbox" id="fm"> m (multiline)</label>
</div>
<label>Test string</label>
<textarea id="teststr" placeholder="Paste text here to test against your regex...">Contact us at hello@example.com or support@test.org for help.</textarea>
<div class="result" id="result"></div>
</div>
<div class="footer">Built by Atreus — dev.tools collection</div>
<script>
document.getElementById('pattern').addEventListener('input', run);
document.getElementById('teststr').addEventListener('input', run);
document.getElementById('fg').addEventListener('change', run);
document.getElementById('fi').addEventListener('change', run);
document.getElementById('fm').addEventListener('change', run);
function run() {
const pat = document.getElementById('pattern').value;
const str = document.getElementById('teststr').value;
const el = document.getElementById('result');
if (!pat) { el.innerHTML = '<span style="color:#484f58">Enter a pattern above</span>'; return; }
let flags = '';
if (document.getElementById('fg').checked) flags += 'g';
if (document.getElementById('fi').checked) flags += 'i';
if (document.getElementById('fm').checked) flags += 'm';
let re;
try { re = new RegExp(pat, flags); }
catch(e) { el.innerHTML = `<span class="error">Invalid regex: ${e.message}</span>`; return; }
// Find matches
const matches = [];
if (flags.includes('g')) {
let m;
while ((m = re.exec(str)) !== null) {
matches.push({ start: m.index, end: m.index + m[0].length, text: m[0], groups: m.slice(1) });
if (m[0].length === 0) re.lastIndex++;
}
} else {
const m = re.exec(str);
if (m) matches.push({ start: m.index, end: m.index + m[0].length, text: m[0], groups: m.slice(1) });
}
// Highlight
let highlighted = '';
let last = 0;
for (const m of matches) {
highlighted += esc(str.slice(last, m.start));
highlighted += `<mark>${esc(m.text)}</mark>`;
last = m.end;
}
highlighted += esc(str.slice(last));
let html = `<div class="match-text">${highlighted || esc(str)}</div>`;
html += `<div class="stats"><span>${matches.length}</span> match${matches.length !== 1 ? 'es' : ''} found</div>`;
// Groups
if (matches.length > 0 && matches[0].groups.length > 0) {
html += `<div class="stats">Groups: ${matches[0].groups.map((g,i) => `$${i+1}="${g||''}"`).join(', ')}</div>`;
}
// Explain
html += `<div class="explain">${explainRegex(pat)}</div>`;
el.innerHTML = html;
}
function explainRegex(pat) {
const parts = [];
let i = 0;
while (i < pat.length) {
if (pat[i] === '\\' && i+1 < pat.length) {
const c = pat[i+1];
if (c === 'd') parts.push('any digit');
else if (c === 'w') parts.push('any word character');
else if (c === 's') parts.push('any whitespace');
else if (c === 'b') parts.push('word boundary');
else if (c === 'D') parts.push('any non-digit');
else if (c === 'W') parts.push('any non-word character');
else if (c === 'S') parts.push('any non-whitespace');
else if (c === '.') parts.push('literal dot');
else parts.push(`literal "${c}"`);
i += 2;
} else if (pat[i] === '[') {
const end = pat.indexOf(']', i);
if (end > i) { parts.push(`character class ${pat.slice(i, end+1)}`); i = end+1; }
else { parts.push(`"["`); i++; }
} else if (pat[i] === '.') { parts.push('any character'); i++; }
else if (pat[i] === '^') { parts.push('start of string'); i++; }
else if (pat[i] === '$') { parts.push('end of string'); i++; }
else if (pat[i] === '*') { parts.push('zero or more'); i++; }
else if (pat[i] === '+') { parts.push('one or more'); i++; }
else if (pat[i] === '?') { parts.push('optional'); i++; }
else if (pat[i] === '|') { parts.push('OR'); i++; }
else if (pat[i] === '(') { parts.push('group start'); i++; }
else if (pat[i] === ')') { parts.push('group end'); i++; }
else if (pat[i] === '{') {
const end = pat.indexOf('}', i);
if (end > i) { parts.push(`repeat ${pat.slice(i+1, end)} times`); i = end+1; }
else { parts.push(`"{"`); i++; }
}
else { parts.push(`"${pat[i]}"`); i++; }
}
return parts.join(' → ');
}
function esc(s) { return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>'); }
run();
</script>
</body>
</html>