Skip to content

Commit 37ce12a

Browse files
authored
fix(curriculum): added the new version and fixed typos in lec-working-with-regular-exp (freeCodeCamp#63592)
1 parent af13a79 commit 37ce12a

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

curriculum/challenges/english/blocks/lecture-working-with-regular-expressions/6733c5e54e3a154c8078ed48.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ challengeType: 19
55
dashedName: what-are-regex-quantifiers-and-how-do-they-work
66
---
77

8-
# --description--
8+
# --interactive--
99

1010
Let's learn about quantifiers in regular expressions.
1111

@@ -25,6 +25,8 @@ const regex = /^\d{4}$/;
2525

2626
Notice how our quantifier contains only the number `4`. This syntax means "match the previous character exactly four times". Let's see how that behaves:
2727

28+
:::interactive_editor
29+
2830
```js
2931
const regex = /^\d{4}$/;
3032
console.log(regex.test("123")); // false
@@ -34,6 +36,8 @@ console.log(regex.test("123456")); // false
3436
console.log(regex.test("1234567")); // false
3537
```
3638

39+
:::
40+
3741
The pattern only matches the string with exactly four digits, because we have used the anchors and our quantifier only allows exactly four digits. But maybe the identification code only needs to be a minimum of four digits.
3842

3943
To allow for four or more digits, add a comma after the number in your quantifier:
@@ -44,6 +48,8 @@ const regex = /^\d{4,}$/;
4448

4549
Now, our syntax allows the pattern to match four or more digits. Let's test it:
4650

51+
:::interactive_editor
52+
4753
```js
4854
const regex = /^\d{4,}$/;
4955
console.log(regex.test("123")); // false
@@ -53,6 +59,8 @@ console.log(regex.test("123456")); // true
5359
console.log(regex.test("1234567")); // true
5460
```
5561

62+
:::
63+
5664
A seven-digit identifier is rather long. These identifiers should have a maximum of 6 digits, and a minimum of 4 digits. To achieve this, you can add a second number to your quantifier after the comma:
5765

5866
```js
@@ -61,6 +69,8 @@ const regex = /^\d{4,6}$/;
6169

6270
And now our pattern no longer matches the seven-digit identifier, because it is greater than our six-digit maximum.
6371

72+
:::interactive_editor
73+
6474
```js
6575
const regex = /^\d{4,6}$/;
6676
console.log(regex.test("123")); // false
@@ -70,12 +80,14 @@ console.log(regex.test("123456")); // true
7080
console.log(regex.test("1234567")); // false
7181
```
7282

83+
:::
84+
7385
Note that you cannot use this syntax to set a maximum alone – you must always set a minimum. But if you set the minimum to `1`, you can effectively achieve the same result.
7486

7587
We've received updated requirements from our users. Identifiers can now optionally start with a letter. We already know the character class for this, so let's add that to our regular expression:
7688

7789
```js
78-
const regex = /^[a-zA-z]\d{4,6}$/;
90+
const regex = /^[a-zA-Z]\d{4,6}$/;
7991
```
8092

8193
But now we mandate the presence of a letter. How can we make it optional?
@@ -94,6 +106,8 @@ const regex = /^[a-zA-Z]?\d{4,6}$/;
94106

95107
We should validate the result:
96108

109+
:::interactive_editor
110+
97111
```js
98112
const regex = /^[a-zA-Z]?\d{4,6}$/;
99113
console.log(regex.test("123")); // false
@@ -104,6 +118,8 @@ console.log(regex.test("X123456")); // true
104118
console.log(regex.test("1234567")); // false
105119
```
106120

121+
:::
122+
107123
Our pattern now allows for a single optional letter, followed by four to six digits.
108124

109125
Unfortunately, we've just realized we read the requirements wrong. We need to allow for any number of letters before the numbers. We can use our quantifier with a `0` minimum and no defined maximum:
@@ -114,6 +130,8 @@ const regex = /^[a-zA-Z]{0,}\d{4,6}$/;
114130

115131
But our pattern is getting long again. Thankfully, there's another short-hand for "match the previous character zero or more times" – the asterisk (`*`) symbol. Let's replace our quantifier with that in the pattern, and test it:
116132

133+
:::interactive_editor
134+
117135
```js
118136
const regex = /^[a-zA-Z]*\d{4,6}$/;
119137
console.log(regex.test("123")); // false
@@ -124,10 +142,14 @@ console.log(regex.test("X123456")); // true
124142
console.log(regex.test("1234567")); // false
125143
```
126144

145+
:::
146+
127147
Now we successfully match any identifier with zero or more letters followed by four to six numbers. But it turns out this is crashing our system – we actually have to require at least one letter.
128148

129149
Again, we could use a quantifier with a minimum of one and no defined maximum, or we could use yet another special syntax – the plus (`+`) symbol:
130150

151+
:::interactive_editor
152+
131153
```js
132154
const regex = /^[a-zA-Z]+\d{4,6}$/;
133155
console.log(regex.test("123")); // false
@@ -138,6 +160,8 @@ console.log(regex.test("X123456")); // true
138160
console.log(regex.test("1234567")); // false
139161
```
140162

163+
:::
164+
141165
Now the identifiers that do not start with at least one letter fail, regardless of how many numbers there are.
142166

143167
You can use quantifiers to greatly enhance the brevity and readability of your regular expressions.

0 commit comments

Comments
 (0)