var str = 'HF1212 0 34 56 89 HFs2121-PC 1 34 56 HF001 3 45' var pattern = /(HF.+?\s)/g pattern.test(str) console.log(pattern.lastIndex - RegExp.$1.length) pattern.test(str) console.log(pattern.lastIndex - RegExp.$1.length) pattern.test(str) console.log(pattern.lastIndex - RegExp.$1.length) pattern.test(str) console.log(pattern.lastIndex)