/A (Anchored)
What is /A (Anchored)?
/A (Anchored): Forces the pattern to match only at the very start of the subject string (or immediately after the previous match), like an implicit ^. The A modifier anchors a pattern, so it can only match at the start of the text, or right where the previous match ended. It behaves like putting ^ at the beginning, but it also applies to repeated matching.
Frequently asked questions about /A (Anchored)
How is /A (Anchored) used in practice?
You see it mainly in PHP's PCRE functions, as in preg_match('/abc/A', $input). It is useful when you write a small tokenizer that reads a string piece by piece. Each call must continue exactly where the last one stopped, and the anchor prevents the engine from skipping ahead to a later match.
What should you keep in mind with /A (Anchored)?
For everyday validation, ^ and $ are clearer to most readers, so prefer them. Use A when you need its specific behaviour in a loop, and add a comment. Few developers know this flag, and the next person to read the code should not have to look it up.
At a glance
| Category | Regex flags |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |