Replacing all occurrences with regex is overkill. replaceAll() is simpler and more readable.
Old Regex Way:
const text = "Hello World, Hello Universe"; const result = text.replace(/Hello/g, "Hi"); // "Hi World, Hi Universe" // Easy to mess up - forget 'g' flag and only first is replaced!
New replaceAll() Way:
const text = "Hello World, Hello Universe";
const result = text.replaceAll("Hello", "Hi");
// "Hi World, Hi Universe"
// Clear intent, no regex needed
More Examples:
// Remove all spaces
const cleaned = text.replaceAll(" ", "");
// Replace multiple underscores with single hyphen
const formatted = fileName.replaceAll("_", "-");
Browser Support: 94% (Chrome 85+, Firefox 77+, Safari 13.1+)
