Class ReplaceFunction


  • public class ReplaceFunction
    extends FunctionBase
    From http://docs.jsonata.org/string-functions.html: $replace(str, pattern, replacement [, limit]) Finds occurrences of pattern within str and replaces them with replacement. If str is not specified, then the context value is used as the value of str. It is an error if str is not a string. The pattern parameter can either be a string or a regular expression (regex). If it is a string, it specifies the substring(s) within str which should be replaced. If it is a regex, its is used to find. The replacement parameter can either be a string or a function. If it is a string, it specifies the sequence of characters that replace the substring(s) that are matched by pattern. If pattern is a regex, then the replacement string can refer to the characters that were matched by the regex as well as any of the captured groups using a S followed by a number N: * If N = 0, then it is replaced by substring matched by the regex as a whole. * If N > 0, then it is replaced by the substring captured by the Nth parenthesised group in the regex. * If N is greater than the number of captured groups, then it is replaced by the empty string. * A literal $ character must be written as $$ in the replacement string If the replacement parameter is a function, then it is invoked for each match occurrence of the pattern regex. The replacement function must take a single parameter which will be the object structure of a regex match as described in the $match function; and must return a string. The optional limit parameter, is a number that specifies the maximum number of replacements to make before stopping. The remainder of the input beyond this limit will be copied to the output unchanged. Examples $replace("John Smith and John Jones", "John", "Mr")=="Mr Smith and Mr Jones" $replace("John Smith and John Jones", "John", "Mr", 1)=="Mr Smith and John Jones" $replace("abracadabra", "a.*?a", "*")=="*c*bra" $replace("John Smith", "(\w+)\s(\w+)", "$2, $1")=="Smith, John" $replace("265USD", "([0-9]+)USD", "$$$1")=="$265"