Skip to content

Files

Latest commit

 

History

History
22 lines (16 loc) · 560 Bytes

bad-replace-all-arg.md

File metadata and controls

22 lines (16 loc) · 560 Bytes

Pattern: Non-global regex in replaceAll

Issue: -

Description

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.

Examples

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