Haxe has built-in support for regular expressions. They can be used to verify the format of a string, transform a string or extract some regular data from a given text.
Haxe has special syntax for creating regular expressions. We can create a regular expression object by typing it between the ~/
combination and a single /
character:
var r = ~/haxe/i;
Alternatively, we can create regular expression with regular syntax:
var r = new EReg("haxe", "i");
The first argument is a string with a regular expression pattern, the second one is a string with flags (see below).
We can use standard regular expression patterns such as:
.
any character*
repeat zero-or-more+
repeat one-or-more?
optional zero-or-one[A-Z0-9]
character ranges[^\r\n\t]
character not-in-range(...)
parenthesis to match groups of characters^
beginning of the string (beginning of a line in multiline matching mode)$
end of the string (end of a line in multiline matching mode)|
"OR" statement.For example, the following regular expression matches valid email addresses:
~/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z][A-Z][A-Z]*/i;
Please notice that the i
at the end of the regular expression is a flag that enables case-insensitive matching.
The possible flags are the following:
i
case insensitive matchingg
global replace or split, see belowm
multiline matching, ^
and $
represent the beginning and end of a lines
the dot .
will also match newlines (not supported by C# and JavaScript versions before ES2018)u
use UTF-8 matching (Neko and C++ targets only)