regex-tester fully implemented

This commit is contained in:
Domenik 2025-02-20 20:18:56 +01:00
parent 8787ee5c2b
commit 544ebaa7cf
4 changed files with 39 additions and 21 deletions

View file

@ -1,7 +1,8 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
interface RequestBody { interface RequestBody {
input: string; regex: string;
test: string;
} }
export async function regexTest(app: FastifyInstance) { export async function regexTest(app: FastifyInstance) {
@ -16,22 +17,25 @@ export async function regexTest(app: FastifyInstance) {
if (!data) { if (!data) {
return reply.status(400).send({ error: "No Regex declared!" }); return reply.status(400).send({ error: "No Regex declared!" });
} }
const regexPattern = data.input;
// Überprüfe, ob der Regex-Pattern korrekt ist // Überprüfe, ob die Felder regex und test vorhanden sind
let regex: RegExp; if (!data.regex || !data.test) {
return reply.status(400).send({ error: "Regex or test string missing!" });
}
// Versuche, den regulären Ausdruck zu erstellen
let regexPattern;
try { try {
regex = new RegExp(regexPattern); regexPattern = new RegExp(data.regex);
} catch (e) { } catch (e) {
return reply.status(400).send({ error: "Invalid regular expression!" }); return reply.status(400).send({ error: "Invalid regular expression!" });
} }
// Teste den Regex // Teste den Eingabestring gegen das Regex
const result = regex.test(data.input); const result = regexPattern.test(data.test);
// Erstelle eine Ausgabe basierend auf dem Test-Ergebnis // Erstelle die Antwort basierend auf dem Testergebnis
let output = ""; let output = "";
if (result) { if (result) {
output = `The input matches the regular expression!`; output = `The input matches the regular expression!`;
} else { } else {

View file

@ -3,7 +3,7 @@ import type { Metadata } from "next";
import { toolLinks } from "@/constants"; import { toolLinks } from "@/constants";
export const metadata: Metadata = { export const metadata: Metadata = {
title: toolLinks[2].title, title: toolLinks[5].title,
description: "Generator for secure strong passwords!", description: "Generator for secure strong passwords!",
}; };

View file

@ -3,7 +3,7 @@ import type { Metadata } from "next";
import { toolLinks } from "@/constants"; import { toolLinks } from "@/constants";
export const metadata: Metadata = { export const metadata: Metadata = {
title: toolLinks[2].title, title: toolLinks[3].title,
description: "Tester for regular expressions!", description: "Tester for regular expressions!",
}; };

View file

@ -14,7 +14,8 @@ export default function RgbToHex() {
setLoading(true); setLoading(true);
const input = (document.getElementById("input") as HTMLInputElement).value; const regex = (document.getElementById("regex") as HTMLInputElement).value;
const test = (document.getElementById("test") as HTMLInputElement).value;
try { try {
const response = await fetch( const response = await fetch(
@ -24,7 +25,7 @@ export default function RgbToHex() {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ input: input}), body: JSON.stringify({ regex: regex, test: test}),
}, },
); );
if (!response.ok) { if (!response.ok) {
@ -44,24 +45,37 @@ export default function RgbToHex() {
const clearInAndOutput = () => { const clearInAndOutput = () => {
setOutput(""); setOutput("");
const input = document.getElementById("input") as HTMLInputElement; const regex = document.getElementById("regex") as HTMLInputElement;
input.value = ""; const test = document.getElementById("test") as HTMLInputElement;
test.value = "";
regex.value = "";
}; };
return ( return (
<div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center"> <div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center">
<Navbar renderHomeLink={true} /> <Navbar renderHomeLink={true} />
<div className="w-screen h-screen flex flex-col items-center justify-center"> <div className="w-screen h-screen flex flex-col items-center justify-center">
<h2 className="text-5xl font-bold text-white mb-16">password-generator</h2> <h2 className="text-5xl font-bold text-white mb-16">regex-tester</h2>
<div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-row justify-between"> <div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-col justify-between">
<label className="mr-2" htmlFor="input">input:</label> <div>
<label className="mr-2 m" htmlFor="regex">regex:</label>
<input <input
type="text" type="text"
id="input" id="regex"
name="input" name="regex"
className="field-sizing-content bg-black border-1 border-white text-center mb-3"
/>
</div>
<div>
<label className="mr-2" htmlFor="test">test string:</label>
<input
type="text"
id="test"
name="test"
className="field-sizing-content bg-black border-1 border-white text-center" className="field-sizing-content bg-black border-1 border-white text-center"
/> />
</div> </div>
</div>
<div className={"flex flex-row items-center gap-4 mt-4 mb-16"}> <div className={"flex flex-row items-center gap-4 mt-4 mb-16"}>
<Button <Button
content={ content={