check if string is a filename typescript in typescript

index.ts
function isFilename(str: string): boolean {
    // Regular expression to check if the string is a valid filename
    const regex = /^[a-zA-Z0-9\s_@\-^!#$%&+={}\[\]]+\.(jpg|jpeg|png|gif|pdf|docx)$/i;

    return regex.test(str);
}

// Example
const filename = "example.jpg";
if (isFilename(filename)) {
    console.log("Valid filename");
} else {
    console.log("Invalid filename");
}
385 chars
15 lines

related categories

gistlibby LogSnag