Skip to content

Commit bee5f93

Browse files
javascript
1 parent 6e3cfb7 commit bee5f93

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

_javascript/destructuring_assignment.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,28 @@ const [n, ...o] = [1, 2, 3];
8181
console.log(n); // 1
8282
console.log(o); // [2, 3]
8383

84-
// Unpacking values from a regular expression match
84+
// Unpacking values from a regular expression match (using exec())
85+
// exec() = The exec() method executes a search for a match in
86+
// a specified string. Returns a result array, or null.
87+
function parseProtocol(url) {
88+
const parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
89+
if (!parsedURL) {
90+
return false;
91+
}
92+
console.log(parsedURL);
93+
// [
94+
// "https://developer.mozilla.org/en-US/docs/Web/JavaScript",
95+
// "https",
96+
// "developer.mozilla.org",
97+
// "en-US/docs/Web/JavaScript"
98+
// ]
99+
100+
const [
101+
,
102+
protocol,
103+
fullhost,
104+
fullpath
105+
] = parsedURL;
106+
return protocol;
107+
}
108+
console.log(parseProtocol('https://developer.mozilla.org/en-US/docs/Web/JavaScript')); // "https"

0 commit comments

Comments
 (0)