The string to normalize.
Whether to preserve case sensitivity.
The normalized string.
export function normalizeString(text: string, caseSensitive = false): string {
if (!text) return "";
// Replace special characters and normalize spacing
let normalized = text
.replace(/[^\w\s]/gi, " ") // Replace special chars with space
.replace(/\s+/g, " ") // Collapse multiple spaces
.trim();
if (!caseSensitive) {
normalized = normalized.toLowerCase();
}
return normalized;
}
Cleans and normalizes a string for comparison.