16 chars · 1 line
8 chars · 1 line
16 chars · 1 line
8 chars · 1 line
Regex replacement behavior
JavaScript regex replacement without g changes only the first match. In the sample, the first whitespace run becomes a hyphen while the second remains. Adding g makes the same replacement operate on every match. This tool is free to use. No account or payment is required.
Last updated
String.replace observes the RegExp's global flag. Without g it stops after the first regex match; with g it continues through the input using the same pattern and replacement string.
Use the same whitespace pattern and replacement, then add g.
one-two threeone-two-threeCommon questions
A RegExp without the g flag replaces only its first match.
This tool intentionally demonstrates native regex String.replace semantics; adding g is the normal regex way to replace all matches with that API.
Yes. Each supported flag controls a separate aspect of matching, and each flag may appear only once.
Debugging a specific Regex issue? Browse Regex troubleshooting.