Requires the Hack Standard Library to be installed.
Facebook Engineer?

This function is available as Regex\replace_with() in Facebook's www repository.

Returns $haystack with any substring matching $pattern replaced by the result of $replace_func applied to that match

namespace HH\Lib\Regex;

function replace_with<T as Match>(
  string $haystack,
  Pattern<T> $pattern,
  (function(T): string) $replace_func,
  int $offset = 0,
): string;

If $offset is given, replacements are made only starting from $offset.

Throws Invariant[Violation]Exception if $offset is not within plus/minus the length of $haystack.


  • string $haystack
  • Pattern<T> $pattern
  • (function(T): string) $replace_func
  • int $offset = 0


  • string