Language: EN

regex-banderas

Flags in Regex

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:

SymbolFunction
gGlobal mode, searches for all matches
iIgnores case sensitivity
mMulti-line mode
sAllows the dot (.) to match newline characters
xAllows the inclusion of whitespace and comments
uAllows 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).

The year is 2024 and the day is 24.

This is the default behavior in many languages

Modifier i (Case Insensitive)

Ignores case sensitivity in the search.

Hola, hola, and HOLA to everyone!

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)

La first line

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.

Hola

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.

ñ, é, and ß are valid Unicode letters.

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 #.

We found 1234 and 5678 in the document.

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