String to normalize.
Whether to preserve case (default: false).
Normalized string with special characters and extra whitespace removed.
export function normalizeString(text: string, isCaseSensitive = false): string {
if (!text) return "";
// Replace special characters and normalize spacing
const replaced = text
.replaceAll(/[^\w\s]/gi, " ") // Replace special chars with space
.replaceAll(/\s+/g, " ") // Collapse multiple spaces
.trim();
return isCaseSensitive ? replaced : replaced.toLowerCase();
}
Normalizes a string by removing special characters and collapsing whitespace. Optionally converts to lowercase based on case sensitivity setting.