Created
August 2, 2020 09:00
-
-
Save lokshunhung/88b5c0752204a54d649b47fdb4965a84 to your computer and use it in GitHub Desktop.
JavaScript: String.prototype.match & RegExp.prototype.exec
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
| // A scratchpad testing the differences between: | |
| // - `String.prototype.match` (with & without `g` flag) | |
| // - `RegExp.prototype.exec` (with & without `g` flag) | |
| const str = "A string\nWith 2 endlines\n"; | |
| (function () { | |
| console.info("==== String.prototype.match GLOBAL ===="); | |
| const re = /(\w)(?<NamedGroup1>\w)\n/g; | |
| // Returns an array containing **all** string segments matching the **whole regex** | |
| // Extra properties **NOT included** | |
| // RegExp is non-stateful | |
| // Capturing groups are **NOT included** | |
| console.log(str.match(re)); // ["ng\n", "es\n"] | |
| console.log(str.match(re)); // ["ng\n", "es\n"] | |
| console.log(str.match(re)); // ["ng\n", "es\n"] | |
| })(); | |
| (function () { | |
| console.info("==== String.prototype.match ONCE ===="); | |
| const re = /(\w)(?<NamedGroup1>\w)\n/; | |
| // Returns an array containing the **first** match of the whole regex, followed by **other capturing groups** | |
| // Extra properties included at the array object: "index", "input", "groups" | |
| // RegExp is non-stateful | |
| // Capturing groups are included | |
| console.log(str.match(re)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}) | |
| console.log(str.match(re)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}) | |
| console.log(str.match(re)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}) | |
| })(); | |
| (function () { | |
| console.info("==== RegExp.prototype.exec GLOBAL ===="); | |
| const re = /(\w)(?<NamedGroup1>\w)\n/g; | |
| // Returns an array containing the **successive** match of the whole regex, followed by **other capturing groups** | |
| // Extra properties included at the array object: "index", "input", "groups" | |
| // RegExp is **Stateful** | |
| // Capturing groups are included | |
| console.log(re.exec(str)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}); | |
| console.log(re.exec(str)); // Object.assign(["es\n", "e", "s"], {index: 22, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "s"}}); | |
| console.log(re.exec(str)); // null | |
| })(); | |
| (function () { | |
| console.info("==== RegExp.prototype.exec ONCE ===="); | |
| const re = /(\w)(?<NamedGroup1>\w)\n/; | |
| // Returns an array containing the **first** match of the whole regex, followed by **other capturing groups** | |
| // Extra properties included at the array object: "index", "input", "groups" | |
| // RegExp is non-stateful(?) | |
| // Capturing groups are included | |
| console.log(re.exec(str)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}); | |
| console.log(re.exec(str)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}); | |
| console.log(re.exec(str)); // Object.assign(["ng\n", "n", "g"], {index: 6, input: "A string\nWith 2 endlines\n", groups: {NamedGroup1: "g"}}); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment