move regexp creation into the if-statement, and fix missing var