A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

The RegEx function wraps a Pattern object and provides a useful set of functions to simplify text processing.

1 Processing text

Assume you want to search and replace a identification tag with to form #TEXT-NUMBER. The regular expresion for that is #\w+-\d+

You can use RegEx to do quick text processing to replace the matching group.

var input = "First: #AA-1010, Second #AB-2020";
return new Ax.util.RegEx("#\\w+-\\d+").parse(input, (matcher, _) => {
	var tag = matcher.group(0);
	return Ax.lang.String.format("<a href='http://data/%s'>%s</a>", tag, tag).toString();
First: <a href='http://data/#AA-1010'>#AA-1010</a>, Second <a href='http://data/#AB-2020'>#AB-2020</a>
Notice that regular expression special characters like \ should be escaped

2 Using specific groups

The previous example can be a bit more sophisticated. Assume we want to handle TEXT and NUMBER as individual elements. Then the regular expression must have groups like #(\w+)-(\d+).

var input = "First: #AA-1010, Second #AB-2020";
return new Ax.util.RegEx("#(\\w+)-(\\d+)").parse(input, matcher => {
    var tagText   = matcher.group(1);
    var tagNumber = parseInt(matcher.group(2));
    return Ax.lang.String.format("<a href='http://data/%d'>%s-%d</a>", tagNumber, tagText, tagNumber).toString();
First: <a href='http://data/1010'>AA-1010</a>, Second <a href='http://data/2020'>AB-2020</a>