Express.js Code Snippets
Quick backend API snippets using Express.js.
function sanitizeFilename(filename) {
// Remove the extension
const nameWithoutExt = filename.replace(/\.[^/.]+$/, '')
// Replace all non-alphanumeric characters with hyphens
const sanitized = nameWithoutExt.replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-+|-+$/g, '')
return sanitized
}
// Example usage
const input = "Mahbub-hasan.Hira.jpg";
const result = sanitizeFilename(input);
console.log(result); // Output: Mahbub-hasan-Hira
Get the filename without extension
1. firstly we remove the extension by r...const generateUsername = email =>
email.split('@')[0].replace(/[^a-zA-Z0-9]/g, '') + '_' + Math.random().toString(36).slice(2, 6);
// Example
console.log(generateUsername('john.doe@example.com')); // output ==> johndoe_ab3x