Flags are options we can apply to a regular expression to change its matching behavior.
These options can be specified at the beginning of the regular expression, after a forward slash or in parentheses, depending on the programming language used.
Some of the most common flags are:
Symbol | Function | ||
---|---|---|---|
g | Global mode, searches for all matches | ||
i | Ignores case sensitivity | ||
m | Multi-line mode | ||
s | Allows the dot (. ) to match newline characters | ||
x | Allows the inclusion of whitespace and comments | ||
u | Allows the use of Unicode characters |
Not all of them will be available in all languages
Modifier g (Global Match)
Matches throughout the entire text (not just the first occurrence).
This is the default behavior in many languages
Modifier i (Case Insensitive)
Ignores case sensitivity in the search.
Here, the Regex selects all variants of “hola”, regardless of case.
Modifier m (Multi-line)
Allows ^
and $
to match the start and end of lines (not just the entire string)
A line that does not start with La
La third line
Here, ^La
selects the word “La” that appears at the beginning of each line, not just at the start of the text.
Modifier s (Dot All)
Allows the dot (.
) to match newline characters (\n
), in addition to any other character.
some things in between
mundo!
Here, the regex selects the text between “Hola” and “mundo”, even crossing lines due to the s
modifier.
Modifier u (Unicode)
Enables full matching of Unicode characters.
Here, \p{L}
selects any Unicode letter, such as ñ
, é
, and ß
.
Modifier x (Verbose)
Allows comments and whitespace within the regex for better readability. Comments are placed with #
.
Here, all numbers with more than one digit are selected, while the comment after #
does not affect the regex.
Do not overuse this, as it generally complicates things instead of improving them