import { useRef, useState } from "react"; export const RegexPlayground = () => { const regexRef = useRef(null); const [output, setOutput] = useState>(); const onInput = (text: string) => { if (!regexRef || !regexRef.current) return; const regexp = new RegExp(regexRef.current.value, "g"); const results: Array = []; text.split("\n").forEach((line, index) => { const matches = line.matchAll(regexp); let lastIndex = 0; for (const match of matches) { if (match.index === undefined) continue; if (!match.length) continue; const start = match.index; let length = 0; match.forEach((group) => length += group.length); results.push( {line.substring(lastIndex, start)} {line.substring(start, start + length)} ); lastIndex = start + length; } if (lastIndex < line.length) { results.push( {line.substring(lastIndex)} ); } if (lastIndex > 0) results.push(
); }); setOutput(results); }; return (

Application

Regex playground. Experiment with your filters here. WIP.

onInput(e.currentTarget.innerText ?? "")} contentEditable >

Matches

{output}

); };