"use client"; import { useState, useEffect } from "react"; import { Code, AlertCircle, CheckCircle2 } from "lucide-react"; import { Textarea } from "@/components/ui/textarea"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Switch } from "@/components/ui/switch"; import { cn } from "@/lib/utils"; export default function RegexPage() { const [pattern, setPattern] = useState("([a-zA-Z0-9._%-]+)@([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})"); const [flags, setFlags] = useState({ global: true, ignoreCase: false, multiline: false }); const [text, setText] = useState("My email is example@mail.com and work@company.org."); const [matches, setMatches] = useState([]); const [error, setError] = useState(null); useEffect(() => { try { if (!pattern) { setMatches([]); setError(null); return; } const flagStr = (flags.global ? 'g' : '') + (flags.ignoreCase ? 'i' : '') + (flags.multiline ? 'm' : ''); const regex = new RegExp(pattern, flagStr); const allMatches = []; let match; if (flags.global) { while ((match = regex.exec(text)) !== null) { allMatches.push(match); if (match.index === regex.lastIndex) regex.lastIndex++; // Prevent infinite loop for zero-width matches } } else { match = regex.exec(text); if (match) allMatches.push(match); } setMatches(allMatches); setError(null); } catch (e: any) { setError(e.message); setMatches([]); } }, [pattern, flags, text]); return (

正则表达式测试

测试和调试正则表达式,支持实时匹配预览

配置正则
/
setPattern(e.target.value)} className={cn("pl-6 pr-10 font-mono", error && "border-destructive focus-visible:ring-destructive")} placeholder="输入正则表达式模式..." />
/
setFlags({...flags, global: v})} title="全局匹配" /> setFlags({...flags, ignoreCase: v})} title="忽略大小写" /> setFlags({...flags, multiline: v})} title="多行模式" />
{error && (
{error}
)}
测试文本