Skip to content

Instantly share code, notes, and snippets.

@lokshunhung
Created August 2, 2020 09:00
Show Gist options
  • Select an option

  • Save lokshunhung/88b5c0752204a54d649b47fdb4965a84 to your computer and use it in GitHub Desktop.

Select an option

Save lokshunhung/88b5c0752204a54d649b47fdb4965a84 to your computer and use it in GitHub Desktop.
JavaScript: String.prototype.match & RegExp.prototype.exec
// 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