CoolFace
Apppublic

admin08077/Githubgemini

sourceHugging Faceotherupdated 10mo agoView on Hugging Face
0likes
RegexSandbox-C_fgP-Gg.js3 linesDownload Raw Back to assets
1import{r as l,j as e}from"./react-DKy9e2uO.js";import{g as b}from"./geminiService-D2PWX7SA.js";import{B as N}from"./index-Ykf-MVva.js";import{L as y}from"./LoadingSpinner-B20BJ_HS.js";import"./bottleneck-Cpj98o6Y.js";import"./@google-D80DdW2m.js";import"./react-dom-CpxHE_eW.js";import"./scheduler-DYLXRpC5.js";import"./idb-Dob3nYDb.js";import"./marked-CesSW9Du.js";import"./jszip-s56H2EZ-.js";const v=[{name:"Email",pattern:"/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/g"},{name:"URL",pattern:"/https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)/g"},{name:"IPv4 Address",pattern:"/((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}/g"},{name:"Date (YYYY-MM-DD)",pattern:"/\\d{4}-\\d{2}-\\d{2}/g"}],w=()=>e.jsxs("div",{className:"bg-surface border border-border p-4 rounded-lg",children:[e.jsx("h3",{className:"text-lg font-bold mb-2",children:"Regex Cheat Sheet"}),e.jsxs("div",{className:"grid grid-cols-2 gap-x-4 gap-y-1 text-xs font-mono",children:[e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"."})," - Any character"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"\\d"})," - Any digit"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"\\w"})," - Word character"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"\\s"})," - Whitespace"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"[abc]"})," - a, b, or c"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"[^abc]"})," - Not a, b, or c"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"*"})," - 0 or more"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"+"})," - 1 or more"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"?"})," - 0 or one"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"^"})," - Start of string"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"$"})," - End of string"]}),e.jsxs("p",{children:[e.jsx("span",{className:"text-primary",children:"\\b"})," - Word boundary"]})]})]}),I=({initialPrompt:d})=>{const[i,x]=l.useState("/\\b([A-Z][a-z]+)\\s(\\w+)\\b/g"),[o,u]=l.useState("The quick Brown Fox jumps over the Lazy Dog."),[p,f]=l.useState(d||"find capitalized words and the word after"),[h,g]=l.useState(!1),{matches:a,error:c}=l.useMemo(()=>{try{const r=i.match(/^\/(.*)\/([gimyus]*)$/);if(!r)return{matches:null,error:"Invalid regex literal. Use /pattern/flags."};const[,t,s]=r,n=new RegExp(t,s);return{matches:[...o.matchAll(n)],error:null}}catch(r){return{matches:null,error:r instanceof Error?r.message:"Unknown error."}}},[i,o]),m=l.useCallback(async r=>{if(r){g(!0);try{const t=b(r);let s="";for await(const n of t)s+=n;x(s.trim().replace(/^`+|`+$/g,""))}finally{g(!1)}}},[]);l.useEffect(()=>{d&&m(d)},[d,m]);const j=l.useMemo(()=>{if(!a||a.length===0||c)return o;let r=0;const t=[];return a.forEach((s,n)=>{s.index!==void 0&&(t.push(o.substring(r,s.index)),t.push(e.jsx("mark",{className:"bg-primary/20 text-primary rounded px-1",children:s[0]},n)),r=s.index+s[0].length)}),t.push(o.substring(r)),t},[a,o,c]);return e.jsxs("div",{className:"h-full flex flex-col p-4 sm:p-6 lg:p-8 text-text-primary",children:[e.jsxs("header",{className:"mb-6",children:[e.jsxs("h1",{className:"text-3xl font-bold flex items-center",children:[e.jsx(N,{}),e.jsx("span",{className:"ml-3",children:"RegEx Sandbox"})]}),e.jsx("p",{className:"text-text-secondary mt-1",children:"Test your regular expressions and generate them with AI."})]}),e.jsxs("div",{className:"flex-grow grid grid-cols-1 lg:grid-cols-3 gap-6 min-h-0",children:[e.jsxs("div",{className:"lg:col-span-2 flex flex-col gap-4",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx("input",{type:"text",value:p,onChange:r=>f(r.target.value),placeholder:"Describe the pattern to find...",className:"flex-grow px-3 py-1.5 rounded-md bg-surface border border-border text-sm focus:ring-2 focus:ring-primary"}),e.jsx("button",{onClick:()=>m(p),disabled:h,className:"btn-primary px-4 py-1.5 flex items-center",children:h?e.jsx(y,{}):"Generate"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"regex-pattern",className:"text-sm font-medium text-text-secondary",children:"Regular Expression"}),e.jsx("input",{id:"regex-pattern",type:"text",value:i,onChange:r=>x(r.target.value),className:`w-full mt-1 px-3 py-2 rounded-md bg-surface border ${c?"border-red-500":"border-border"} font-mono text-sm focus:ring-2 focus:ring-primary`}),c&&e.jsx("p",{className:"text-red-500 text-xs mt-1",children:c})]}),e.jsxs("div",{className:"flex flex-col flex-grow min-h-0",children:[e.jsx("label",{htmlFor:"test-string",className:"text-sm font-medium text-text-secondary",children:"Test String"}),e.jsx("textarea",{id:"test-string",value:o,onChange:r=>u(r.target.value),className:"w-full mt-1 p-3 rounded-md bg-surface border border-border font-mono text-sm resize-y h-32"}),e.jsx("div",{className:"mt-2 p-3 bg-background rounded-md border border-border min-h-[50px] whitespace-pre-wrap",children:j})]}),e.jsxs("div",{className:"flex-shrink-0",children:[e.jsxs("h3",{className:"text-lg font-bold",children:["Match Groups (",(a==null?void 0:a.length)||0,")"]}),e.jsx("div",{className:"mt-2 p-2 bg-surface rounded-md overflow-y-auto max-h-48 font-mono text-xs border border-border",children:a&&a.length>0?a.map((r,t)=>e.jsxs("details",{className:"p-2 border-b border-border",children:[e.jsxs("summary",{className:"cursor-pointer text-green-700",children:["Match ",t+1,': "',r[0],'"']}),e.jsx("div",{className:"pl-4 mt-1",children:Array.from(r).map((s,n)=>e.jsxs("p",{className:"text-text-secondary",children:["Group ",n,": ",e.jsx("span",{className:"text-amber-700",children:String(s)})]},n))})]},t)):e.jsx("p",{className:"text-text-secondary text-sm p-2",children:"No matches found."})})]})]}),e.jsxs("div",{className:"lg:col-span-1 space-y-4",children:[e.jsx(w,{}),e.jsxs("div",{className:"bg-surface border border-border p-4 rounded-lg",children:[e.jsx("h3",{className:"text-lg font-bold mb-2",children:"Common Patterns"}),e.jsx("div",{className:"flex flex-col items-start gap-2",children:v.map(r=>e.jsx("button",{onClick:()=>x(r.pattern),className:"text-left text-sm text-primary hover:underline",children:r.name},r.name))})]})]})]})]})};export{I as RegexSandbox};2//# sourceMappingURL=RegexSandbox-C_fgP-Gg.js.map3