How do I check if a value is an empty or whitespace-only string in JavaScript using regular expressions?
Davide S
When converting a string to a slug in JavaScript, you typically want to transform the string into a URL-friendly format that is suitable for use in URLs or as identifiers. The process involves removing special characters, converting spaces to hyphens, and ensuring that the resulting slug is all lowercase. However, if you want to preserve non-ASCII characters, such as accented characters or characters from non-Latin scripts, you need to handle them differently. Here's an approach to convert a string to a slug while preserving non-ASCII characters:
1. Normalize the string: Use thenormalize() method to normalize the string and ensure consistent representation of accented characters and other Unicode characters. This helps to handle variations in character encoding.
2. Replace non-ASCII characters: Use thereplace() method with a regular expression to replace non-ASCII characters with their closest ASCII equivalents or remove them entirely. You can achieve this by using a library likeunidecode or by manually defining a mapping of non-ASCII characters to their ASCII equivalents.
3. Remove special characters: Use thereplace() method with a regular expression to remove special characters, such as punctuation marks or symbols, from the string. You can use a regular expression that matches any non-alphanumeric character and replace it with an empty string.
4. Convert spaces to hyphens: Use thereplace() method with a regular expression to replace spaces with hyphens ("-") in the string. This step ensures that the slug is URL-friendly.
5. Convert to lowercase: Use thetoLowerCase() method to convert the entire string to lowercase. This step helps maintain consistency and avoids confusion due to case sensitivity.
Here's an example implementation of a function that converts a string to a slug while preserving non-ASCII characters:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function convertToSlug(str) {
const normalizedStr = str.normalize("NFD"); // Normalize the string
const asciiStr = normalizedStr
.replace(/[^\x00-\x7F]/g, "") // Replace non-ASCII characters
.replace(/[^\w\s-]/g, "") // Remove special characters
.replace(/\s+/g, "-") // Convert spaces to hyphens
.toLowerCase(); // Convert to lowercase
return asciiStr;
}
// Example usage
const input = "Café au Lait"; // Non-ASCII characters: é
const slug = convertToSlug(input);
console.log(slug); // Output: cafe-au-lait
In this example, the functionconvertToSlug() takes a string as input and performs the necessary steps to create a slug. The string is first normalized usingnormalize("NFD") to handle any variations in character encoding. Then, non-ASCII characters are removed or replaced using regular expressions. Special characters are removed, spaces are converted to hyphens, and the final slug is converted to lowercase.
By following this approach, you can convert a string to a slug while preserving non-ASCII characters, ensuring that the resulting slug is still readable and meaningful.