| Level Navigation: 1 | (2ℹ️) | 3 | 4 | 5 | 6 | (7ℹ️) | (8ℹ️) | (9ℹ️) | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | (37ℹ️) | 38⚡ | 39⚡ | 40⚡ | 41⚡ | 42 | 43⚡ | 44⚡ | 45 | 46 | (47ℹ️) |
Now we have a lot of replaceAll() calls! We can simplify this using a regular expression (regex) to match multiple characters at once.
A regex lets us match a pattern of characters. We can use square brackets [] to match any of the characters inside:
export function toSnakeCase(text) {
return text.replaceAll(' ', '_').replaceAll(/[!?,]/g, '_').toLowerCase();
}
What does this regex do?
/[!?,]/g is a regular expression pattern with the global flag[] mean “match any of these characters”!?, means match exclamation marks, question marks, or commas/ mark the start and end of the regex patterng flag makes the regex global so replaceAll() can replace every matchTry it now: Update your function with this regex and run your tests. They should all still pass (green)!