JSRegEx::ReplaceCallback (open)

Replace every occurrence of a regular expression by the result of a callback function

Syntax

LOADLIB "wh::regex.whlib";

STRING FUNCTION ReplaceCallback(STRING input, FUNCTION PTR replacefunc)

Parameters

STRING input

Input to do the replace in

FUNCTION PTR replacefunc

String function that takes a match and returns a replacement

Return value

STRING

Input with every occurance of the regular expression replaced

Description

This function can be used to find every occurence of a regular expression within a string, and call a function for each match. If the function returns a string, the matching occurence is replaced by the function result. If the callback function is a macro, the text is not replaced. The callback function must take four arguments: a string which contains the matching text from the input; a string array containing a string for each subexpression (empty if the subexpression was not matched); an integer with the position of the matched text within the input (before replacement); and a string containing the original input text. If the global flag was set on the regular expression, the callback is only called for the first match.