Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Created March 16, 2018 02:41
Show Gist options
  • Save a1exlism/3253e86336e7d843e57166065b2b23cc to your computer and use it in GitHub Desktop.
Save a1exlism/3253e86336e7d843e57166065b2b23cc to your computer and use it in GitHub Desktop.
let str = 'PathofExile \nThe world path of Game';
// match: Reg with/withou `g` flag
let reg = /(path)\s*/ig;
let reg2 = /(path)\s*/i;
let result1 = str.match(reg);
console.log(reg.lastIndex); // 0
// match with global
console.log(result1);
// 0-x: every matched string
console.log(reg.lastIndex); // 0 Reason: reg is the `Formal Parameter`
// match without global
let result2 = str.match(reg2);
console.log(result2);
// 0: whole string 1-x: group match
let result3 = reg.exec(str);
console.log(reg.lastIndex); // 4
let result4 = reg2.exec(str);
console.log(reg.lastIndex); // 0 Explanation: `without` global flag, the lastIndex `always` reset to 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment