Prev | Current Page 408 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

The following
expression defines a group that is not used by the split() method.
class Main
{
public static function main()
{
var re = ~/b(c)d/g;
var a = re.split(???abcdeabcde??™);
trace(a); // [a, ea, e]
}
}
The replace() method substitutes the matched expression with a second argument passed to the function.
The g modifier applies the replacement on the whole string.
class Main
{
public static function main()
{
var re = ~/b(c)d/g;
var s = re.replace(???abcdeabcde??™, ???x??™);
trace(s); // axeaxe
}
}
Part II: Server Side, JavaScript, and Flash; Oh My!
214
Groups can be used in replacement to create back - references. A back - reference contains the value of a
matched group and can be used in the replacement string. The back - references have the $ prefix
followed by the numeric id of the group. Back - references are handy in replacements when you do not
want to replace a portion of the string but want to wrap its value between some characters.
class Main
{
public static function main()
{
var re = ~/-([^-])-/g;
var s = re.


Pages:
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420