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
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>regex.test — Live regex tester with explanations</title>
|
| 7 |
+
<meta name="description" content="Test regular expressions in real-time with match highlighting and plain English explanations. Free, fast, no signup.">
|
| 8 |
+
<style>
|
| 9 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 10 |
+
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; }
|
| 11 |
+
h1 { font-size: 2rem; color: #f0f6fc; margin-bottom: 0.25rem; }
|
| 12 |
+
.sub { color: #484f58; margin-bottom: 2rem; }
|
| 13 |
+
.panel { width: 100%; max-width: 700px; }
|
| 14 |
+
label { color: #8b949e; font-size: 0.85rem; display: block; margin-bottom: 0.35rem; margin-top: 1rem; }
|
| 15 |
+
.regex-row { display: flex; gap: 0.5rem; align-items: center; }
|
| 16 |
+
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%; }
|
| 17 |
+
input:focus, textarea:focus { border-color: #58a6ff; }
|
| 18 |
+
.flags { display: flex; gap: 0.75rem; margin-top: 0.5rem; }
|
| 19 |
+
.flags label { display: flex; align-items: center; gap: 0.3rem; cursor: pointer; color: #8b949e; margin-top: 0; }
|
| 20 |
+
.flags input[type=checkbox] { accent-color: #58a6ff; }
|
| 21 |
+
textarea { min-height: 120px; resize: vertical; line-height: 1.6; }
|
| 22 |
+
.result { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 1.25rem; margin-top: 1rem; line-height: 1.8; font-size: 0.95rem; }
|
| 23 |
+
.match-text { font-family: 'Courier New', monospace; line-height: 2; word-break: break-all; }
|
| 24 |
+
.match-text mark { background: rgba(88,166,255,0.25); color: #58a6ff; border-radius: 2px; padding: 0.1rem 0.2rem; }
|
| 25 |
+
.stats { color: #8b949e; font-size: 0.85rem; margin-top: 0.75rem; }
|
| 26 |
+
.stats span { color: #3fb950; }
|
| 27 |
+
.explain { color: #d2a8ff; margin-top: 0.75rem; font-size: 0.9rem; }
|
| 28 |
+
.error { color: #f85149; }
|
| 29 |
+
.footer { margin-top: 3rem; color: #30363d; font-size: 0.75rem; }
|
| 30 |
+
</style>
|
| 31 |
+
</head>
|
| 32 |
+
<body>
|
| 33 |
+
<h1>regex.test</h1>
|
| 34 |
+
<p class="sub">Live matching with explanations.</p>
|
| 35 |
+
|
| 36 |
+
<div class="panel">
|
| 37 |
+
<label>Pattern</label>
|
| 38 |
+
<div class="regex-row">
|
| 39 |
+
<span style="color:#484f58">/</span>
|
| 40 |
+
<input id="pattern" type="text" placeholder="[a-z]+@[a-z]+\.[a-z]{2,}">
|
| 41 |
+
<span style="color:#484f58">/</span>
|
| 42 |
+
</div>
|
| 43 |
+
<div class="flags">
|
| 44 |
+
<label><input type="checkbox" id="fg" checked> g (global)</label>
|
| 45 |
+
<label><input type="checkbox" id="fi"> i (case-insensitive)</label>
|
| 46 |
+
<label><input type="checkbox" id="fm"> m (multiline)</label>
|
| 47 |
+
</div>
|
| 48 |
+
|
| 49 |
+
<label>Test string</label>
|
| 50 |
+
<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>
|
| 51 |
+
|
| 52 |
+
<div class="result" id="result"></div>
|
| 53 |
+
</div>
|
| 54 |
+
|
| 55 |
+
<div class="footer">Built by Atreus — dev.tools collection</div>
|
| 56 |
+
|
| 57 |
+
<script>
|
| 58 |
+
document.getElementById('pattern').addEventListener('input', run);
|
| 59 |
+
document.getElementById('teststr').addEventListener('input', run);
|
| 60 |
+
document.getElementById('fg').addEventListener('change', run);
|
| 61 |
+
document.getElementById('fi').addEventListener('change', run);
|
| 62 |
+
document.getElementById('fm').addEventListener('change', run);
|
| 63 |
+
|
| 64 |
+
function run() {
|
| 65 |
+
const pat = document.getElementById('pattern').value;
|
| 66 |
+
const str = document.getElementById('teststr').value;
|
| 67 |
+
const el = document.getElementById('result');
|
| 68 |
+
|
| 69 |
+
if (!pat) { el.innerHTML = '<span style="color:#484f58">Enter a pattern above</span>'; return; }
|
| 70 |
+
|
| 71 |
+
let flags = '';
|
| 72 |
+
if (document.getElementById('fg').checked) flags += 'g';
|
| 73 |
+
if (document.getElementById('fi').checked) flags += 'i';
|
| 74 |
+
if (document.getElementById('fm').checked) flags += 'm';
|
| 75 |
+
|
| 76 |
+
let re;
|
| 77 |
+
try { re = new RegExp(pat, flags); }
|
| 78 |
+
catch(e) { el.innerHTML = `<span class="error">Invalid regex: ${e.message}</span>`; return; }
|
| 79 |
+
|
| 80 |
+
// Find matches
|
| 81 |
+
const matches = [];
|
| 82 |
+
if (flags.includes('g')) {
|
| 83 |
+
let m;
|
| 84 |
+
while ((m = re.exec(str)) !== null) {
|
| 85 |
+
matches.push({ start: m.index, end: m.index + m[0].length, text: m[0], groups: m.slice(1) });
|
| 86 |
+
if (m[0].length === 0) re.lastIndex++;
|
| 87 |
+
}
|
| 88 |
+
} else {
|
| 89 |
+
const m = re.exec(str);
|
| 90 |
+
if (m) matches.push({ start: m.index, end: m.index + m[0].length, text: m[0], groups: m.slice(1) });
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// Highlight
|
| 94 |
+
let highlighted = '';
|
| 95 |
+
let last = 0;
|
| 96 |
+
for (const m of matches) {
|
| 97 |
+
highlighted += esc(str.slice(last, m.start));
|
| 98 |
+
highlighted += `<mark>${esc(m.text)}</mark>`;
|
| 99 |
+
last = m.end;
|
| 100 |
+
}
|
| 101 |
+
highlighted += esc(str.slice(last));
|
| 102 |
+
|
| 103 |
+
let html = `<div class="match-text">${highlighted || esc(str)}</div>`;
|
| 104 |
+
html += `<div class="stats"><span>${matches.length}</span> match${matches.length !== 1 ? 'es' : ''} found</div>`;
|
| 105 |
+
|
| 106 |
+
// Groups
|
| 107 |
+
if (matches.length > 0 && matches[0].groups.length > 0) {
|
| 108 |
+
html += `<div class="stats">Groups: ${matches[0].groups.map((g,i) => `$${i+1}="${g||''}"`).join(', ')}</div>`;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// Explain
|
| 112 |
+
html += `<div class="explain">${explainRegex(pat)}</div>`;
|
| 113 |
+
el.innerHTML = html;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
function explainRegex(pat) {
|
| 117 |
+
const parts = [];
|
| 118 |
+
let i = 0;
|
| 119 |
+
while (i < pat.length) {
|
| 120 |
+
if (pat[i] === '\\' && i+1 < pat.length) {
|
| 121 |
+
const c = pat[i+1];
|
| 122 |
+
if (c === 'd') parts.push('any digit');
|
| 123 |
+
else if (c === 'w') parts.push('any word character');
|
| 124 |
+
else if (c === 's') parts.push('any whitespace');
|
| 125 |
+
else if (c === 'b') parts.push('word boundary');
|
| 126 |
+
else if (c === 'D') parts.push('any non-digit');
|
| 127 |
+
else if (c === 'W') parts.push('any non-word character');
|
| 128 |
+
else if (c === 'S') parts.push('any non-whitespace');
|
| 129 |
+
else if (c === '.') parts.push('literal dot');
|
| 130 |
+
else parts.push(`literal "${c}"`);
|
| 131 |
+
i += 2;
|
| 132 |
+
} else if (pat[i] === '[') {
|
| 133 |
+
const end = pat.indexOf(']', i);
|
| 134 |
+
if (end > i) { parts.push(`character class ${pat.slice(i, end+1)}`); i = end+1; }
|
| 135 |
+
else { parts.push(`"["`); i++; }
|
| 136 |
+
} else if (pat[i] === '.') { parts.push('any character'); i++; }
|
| 137 |
+
else if (pat[i] === '^') { parts.push('start of string'); i++; }
|
| 138 |
+
else if (pat[i] === '$') { parts.push('end of string'); i++; }
|
| 139 |
+
else if (pat[i] === '*') { parts.push('zero or more'); i++; }
|
| 140 |
+
else if (pat[i] === '+') { parts.push('one or more'); i++; }
|
| 141 |
+
else if (pat[i] === '?') { parts.push('optional'); i++; }
|
| 142 |
+
else if (pat[i] === '|') { parts.push('OR'); i++; }
|
| 143 |
+
else if (pat[i] === '(') { parts.push('group start'); i++; }
|
| 144 |
+
else if (pat[i] === ')') { parts.push('group end'); i++; }
|
| 145 |
+
else if (pat[i] === '{') {
|
| 146 |
+
const end = pat.indexOf('}', i);
|
| 147 |
+
if (end > i) { parts.push(`repeat ${pat.slice(i+1, end)} times`); i = end+1; }
|
| 148 |
+
else { parts.push(`"{"`); i++; }
|
| 149 |
+
}
|
| 150 |
+
else { parts.push(`"${pat[i]}"`); i++; }
|
| 151 |
+
}
|
| 152 |
+
return parts.join(' → ');
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
function esc(s) { return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/\n/g,'<br>'); }
|
| 156 |
+
|
| 157 |
+
run();
|
| 158 |
+
</script>
|
| 159 |
+
</body>
|
| 160 |
+
</html>
|