Skip to content

Commit 02c082d

Browse files
Match (#6987)
* Created match.md * Update match.md * Update match.md * Update match.md * made minor content fixes * Update match.md ---------
1 parent 803ba33 commit 02c082d

File tree

1 file changed

+82
-0
lines changed
  • content/javascript/concepts/strings/terms/match

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
Title: '.match()'
3+
Description: 'Returns an array of matches by matching the string against a regular expression.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Arrays'
9+
- 'Methods'
10+
- 'Strings'
11+
CatalogContent:
12+
- 'introduction-to-javascript'
13+
- 'paths/front-end-engineer-career-path'
14+
---
15+
16+
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.
17+
18+
## Syntax
19+
20+
```pseudo
21+
string.match(regex)
22+
```
23+
24+
**Parameters:**
25+
26+
- `regex`: A regular expression object to match against the string.
27+
28+
**Return value:**
29+
30+
- **If the regular expression has the `g` flag:** Returns an array of all matches found, or `null` if no match is found.
31+
- **Without the `g` flag:** Returns an array with detailed information about the first match (including captured groups), or `null` if no match is found.
32+
33+
## Example 1: Using `.match()` to Find Uppercase Letters in a String
34+
35+
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:
36+
37+
```js
38+
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
39+
const regex = /[A-Z]/g;
40+
console.log(paragraph.match(regex));
41+
```
42+
43+
The output of this code will be:
44+
45+
```shell
46+
[ 'T', 'I' ]
47+
```
48+
49+
If the "g" flag is not used, it returns the array object with the result at index 0:
50+
51+
```js
52+
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
53+
const regex = /[A-Z]/;
54+
console.log(paragraph.match(regex));
55+
```
56+
57+
The output of this code will be:
58+
59+
```shell
60+
[
61+
'T',
62+
index: 0,
63+
input: 'The quick brown fox jumps over the lazy dog. It barked.',
64+
groups: undefined
65+
]
66+
```
67+
68+
## Example 2: Finding All Occurrences of a Word Using `.match()`
69+
70+
This example demonstrates how to use the `.match()` method to find all occurrences of the word "Hello" in a string:
71+
72+
```codebyte/javascript
73+
const myStr = 'Hello Alligators, Hello Devs, how are you?';
74+
const regex = /Hello/g;
75+
console.log(myStr.match(regex))
76+
```
77+
78+
The output of this code is:
79+
80+
```shell
81+
[ 'Hello', 'Hello' ]
82+
```

0 commit comments

Comments
 (0)