Search This Blog

2024/03/21

Javascript Regular Expression:lastindex property of Regular Expression


We can loop over matching parts of string in RegExp.test & get lastindex

var str = "Javascript is an interesting scripting language";
var re = new RegExp( "script", "g" );

var counter =1
while(re.test(str)){
console.log("Test "+ counter+" - Current Index: " + re.lastIndex);
counter++
}

Output:
Test 1 - Current Index: 10
Test 2 - Current Index: 35


No comments:

Post a Comment