Check for nil before using string in regex.

Fixes #2136