From 3daadcae4de66a6d324d70b398611968741d263f Mon Sep 17 00:00:00 2001 From: walkrantrolls Date: Tue, 3 Jun 2025 19:46:37 +0530 Subject: [PATCH 1/6] Created match.md --- .../concepts/strings/terms/match/match.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 content/javascript/concepts/strings/terms/match/match.md diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md new file mode 100644 index 00000000000..6e95fae224e --- /dev/null +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -0,0 +1,50 @@ +--- +Title: '.match()' +Description: 'Returns an array with the items being matches from a provided regular expression found in the string.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'Methods' + - 'Strings' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' +--- + +In JavaScript, the **`.match()`** [method](https://www.codecademy.com/resources/docs/javascript/methods), searches a string for a match against a regular expression, and returns the matches, as an Array object. + +## Syntax + +```pseudo +string.match(regex) +``` + +- `string`: The string to be matched. +- `regex`: A regular expression object, or any object that has a Symbol.match method. + +## Example + +Take the following example: + +```js +const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; +const regex = /[A-Z]/g; +console.log(paragraph.match(regex)); +``` + +The above code produces the following output: + +```shell +["T", "I"] +``` + +## Codebyte Example + +Here is a codebyte example that demonstrates the usage of the `.match()` method: + +```codebyte/javascript +const myStr = 'Hello Alligators, Hello Devs, how are you?'; +const regex = /Hello/g; +console.log(myStr.match(regex)) +``` From 6638bc068dbfcc2bd135068902f637c8104750df Mon Sep 17 00:00:00 2001 From: walkrantrolls Date: Tue, 3 Jun 2025 20:00:03 +0530 Subject: [PATCH 2/6] Update match.md --- content/javascript/concepts/strings/terms/match/match.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md index 6e95fae224e..7da193ccf5f 100644 --- a/content/javascript/concepts/strings/terms/match/match.md +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -25,7 +25,8 @@ string.match(regex) ## Example -Take the following example: +In the following example, a string [variable](https://www.codecademy.com/resources/docs/javascript/variables) called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string +Then, the `.match()` method applies the regular expression to the string and returns an array of all the matches. ```js const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; @@ -33,7 +34,7 @@ const regex = /[A-Z]/g; console.log(paragraph.match(regex)); ``` -The above code produces the following output: +The above code finds all uppercase letters in the `paragraph` string and produces the following output: ```shell ["T", "I"] From 2ed6d121f8a163ee858f9b2a9491ef0510b84ee1 Mon Sep 17 00:00:00 2001 From: walkrantrolls Date: Wed, 4 Jun 2025 12:22:21 +0530 Subject: [PATCH 3/6] Update match.md --- .../concepts/strings/terms/match/match.md | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md index 7da193ccf5f..ce65cd8a5a9 100644 --- a/content/javascript/concepts/strings/terms/match/match.md +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -12,7 +12,7 @@ CatalogContent: - 'paths/front-end-engineer-career-path' --- -In JavaScript, the **`.match()`** [method](https://www.codecademy.com/resources/docs/javascript/methods), searches a string for a match against a regular expression, and returns the matches, as an Array object. +The **`.match()`** method, searches a string for a match against a regular expression, and returns the matches, as an Array object. ## Syntax @@ -23,21 +23,29 @@ string.match(regex) - `string`: The string to be matched. - `regex`: A regular expression object, or any object that has a Symbol.match method. +If no parameter is passed, it will return an Array with an empty string: [""], because the parameter is then equivalent to match(/(?:)/). +If the regular expression does not include the “g” flag, it returns the Array object with the result at index 0. If the “g” flag is used, the resulting Array object contains only the matches and nothing else. + ## Example -In the following example, a string [variable](https://www.codecademy.com/resources/docs/javascript/variables) called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string +In the following example, a string variable called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string Then, the `.match()` method applies the regular expression to the string and returns an array of all the matches. +Find all the uppercase letters in the `paragraph` string: + ```js const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/g; console.log(paragraph.match(regex)); +// Output: ["T", "I"] ``` -The above code finds all uppercase letters in the `paragraph` string and produces the following output: - -```shell -["T", "I"] +If the "g" flag is not used, it returns the Array object with the result at index 0: +```js +const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; +const regex = /[A-Z]/; +console.log(paragraph.match(regex)); +// Output: ['T', index: 0, input: 'The quick brown fox jumps over the lazy dog. It barked.', groups: undefined] ``` ## Codebyte Example From d24703288c9ac9d1433f15e60dc9e708750c3e59 Mon Sep 17 00:00:00 2001 From: walkrantrolls Date: Wed, 4 Jun 2025 12:23:28 +0530 Subject: [PATCH 4/6] Update match.md --- content/javascript/concepts/strings/terms/match/match.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md index ce65cd8a5a9..275aa72cf99 100644 --- a/content/javascript/concepts/strings/terms/match/match.md +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -28,8 +28,8 @@ If the regular expression does not include the “g” flag, it returns the Arra ## Example -In the following example, a string variable called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string -Then, the `.match()` method applies the regular expression to the string and returns an array of all the matches. +In the following example, a string variable called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string. +The `.match()` method applies the regular expression to the string and returns an array of all the matches. Find all the uppercase letters in the `paragraph` string: From 52b45d0ba2b4846241a90ee0afe24d25bcad43b7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 6 Jun 2025 10:32:04 +0530 Subject: [PATCH 5/6] made minor content fixes --- .../concepts/strings/terms/match/match.md | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md index 275aa72cf99..2c4bd318104 100644 --- a/content/javascript/concepts/strings/terms/match/match.md +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -1,10 +1,11 @@ --- Title: '.match()' -Description: 'Returns an array with the items being matches from a provided regular expression found in the string.' +Description: 'Returns an array of matches by matching the string against a regular expression.' Subjects: - 'Computer Science' - 'Web Development' Tags: + - 'Arrays' - 'Methods' - 'Strings' CatalogContent: @@ -12,7 +13,7 @@ CatalogContent: - 'paths/front-end-engineer-career-path' --- -The **`.match()`** method, searches a string for a match against a regular expression, and returns the matches, as an Array object. +The **`.match()`** method searches a string for matches against a regular expression and returns the result as an Array object. The `.match()` method is used in JavaScript to find parts of a string that match a regular expression. It is commonly applied in tasks such as extracting numbers or words, validating formats like email addresses, or parsing structured text. ## Syntax @@ -20,40 +21,62 @@ The **`.match()`** method, searches a string for a match against a regular expre string.match(regex) ``` -- `string`: The string to be matched. -- `regex`: A regular expression object, or any object that has a Symbol.match method. +**Parameters:** -If no parameter is passed, it will return an Array with an empty string: [""], because the parameter is then equivalent to match(/(?:)/). -If the regular expression does not include the “g” flag, it returns the Array object with the result at index 0. If the “g” flag is used, the resulting Array object contains only the matches and nothing else. +- `regex`: A regular expression object to match against the string. -## Example +**Return value:** -In the following example, a string variable called `paragraph` contains a sentence. `regex` defines a regular expression (regex) to match all uppercase letters in the string. -The `.match()` method applies the regular expression to the string and returns an array of all the matches. +- **If the regular expression has the `g` flag:** Returns an array of all matches found, or `null` if no match is found. +- **Without the `g` flag:** Returns an array with detailed information about the first match (including captured groups), or `null` if no match is found. -Find all the uppercase letters in the `paragraph` string: +## Example 1: Using `.match()` to Find Uppercase Letters in a String + +In the following example, a string variable `paragraph` contains a sentence, and `regex` defines a regular expression to match all uppercase letters. The `.match()` method applies this regex to the string and returns an array of all matches: ```js const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/g; console.log(paragraph.match(regex)); -// Output: ["T", "I"] +``` + +The output of this code will be: + +```shell +[ 'T', 'I' ] ``` If the "g" flag is not used, it returns the Array object with the result at index 0: + ```js const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/; console.log(paragraph.match(regex)); -// Output: ['T', index: 0, input: 'The quick brown fox jumps over the lazy dog. It barked.', groups: undefined] ``` -## Codebyte Example +The output of this code will be: -Here is a codebyte example that demonstrates the usage of the `.match()` method: +```shell +[ + 'T', + index: 0, + input: 'The quick brown fox jumps over the lazy dog. It barked.', + groups: undefined +] +``` + +## Example 2: Finding All Occurrences of a Word Using `.match()` + +This example demonstrates how to use the `.match()` method to find all occurrences of the word "Hello" in a string: ```codebyte/javascript const myStr = 'Hello Alligators, Hello Devs, how are you?'; const regex = /Hello/g; console.log(myStr.match(regex)) ``` + +The output of this code is: + +```shell +[ 'Hello', 'Hello' ] +``` From 3afededdbc39950ca2af615d149608a310607655 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 6 Jun 2025 10:33:13 +0530 Subject: [PATCH 6/6] Update match.md --- content/javascript/concepts/strings/terms/match/match.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/javascript/concepts/strings/terms/match/match.md b/content/javascript/concepts/strings/terms/match/match.md index 2c4bd318104..a6a74bec9fc 100644 --- a/content/javascript/concepts/strings/terms/match/match.md +++ b/content/javascript/concepts/strings/terms/match/match.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/front-end-engineer-career-path' --- -The **`.match()`** method searches a string for matches against a regular expression and returns the result as an Array object. The `.match()` method is used in JavaScript to find parts of a string that match a regular expression. It is commonly applied in tasks such as extracting numbers or words, validating formats like email addresses, or parsing structured text. +The **`.match()`** method searches a string for matches against a regular expression and returns the result as an array object. The `.match()` method is used in JavaScript to find parts of a string that match a regular expression. It is commonly applied in tasks such as extracting numbers or words, validating formats like email addresses, or parsing structured text. ## Syntax @@ -46,7 +46,7 @@ The output of this code will be: [ 'T', 'I' ] ``` -If the "g" flag is not used, it returns the Array object with the result at index 0: +If the "g" flag is not used, it returns the array object with the result at index 0: ```js const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';