Pattern: Non-global regex in replaceAll
Issue: -
When using replaceAll
with a regular expression, the regex must include the global flag (g). Without it, replaceAll
will throw a TypeError
since non-global regexes can only replace the first match.
Example of incorrect code:
text.replaceAll(/\s+/, "-");
text.replaceAll(/[aeiou]/, "*");
Example of correct code:
text.replaceAll(/\s+/g, "-");
text.replaceAll(/[aeiou]/g, "*");
text.replaceAll(" ", "-"); // String literal is ok