The replace function’s callback takes the matches as parameters.
For example:
text = text.replace(/<wiki>(.+?)<\/wiki>/g, function(match, contents, offset, input_string)
{
return "<a href="https://stackoverflow.com/questions/3395843/wiki/"+contents.replace(/ /g, "_')+"'>"+contents+"</a>";
}
);
(The second parameter is the first capture group)