Created
March 16, 2018 02:41
-
-
Save a1exlism/3253e86336e7d843e57166065b2b23cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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