Use custom visitor class for optimized url helpers

Rather than trying to use gsub to remove the optional route segments,
which will fail with nested optional segments, use a custom visitor
class that returns a empty string for group nodes.

Closes #9524