PHP preg_replace_callback Javascript equivalent

Usage:

[Explore Code] [Download @GitHub]