Fix index problem when matching using regexp