Method code for $string.global_regexp()

[Turn on line numbering]
arg string, regexp;
var result, location, left, begin_match, end_match;

result = [];
left = string;
while (left) {
    if ((location = match_regexp(left, regexp))) {
        [begin_match, end_match] = location[1];
        result += [begin_match == 1 ? "" : left.subrange(1, begin_match - 1)];
        result += [left.subrange(begin_match, end_match)];
        left = left.subrange(begin_match + end_match);
    } else {
        result += [left];
        left = "";
    }
}
if (!(result.length() % 2))
    result += [""];
return result;

// Created 27-Mar-1995 as a part of ColdCore, see: @help Credit

Tlon