bug fix: reg.lastIndex = 0