You probably want `\A` and `\z` for your regex here rather than `^` and `$` — they'll match the start and end of the string only without matching at newlines.