33> Regular expression is a group of characters or symbols which is used to find a specific pattern from a text.
44
55A regular expression is a pattern that is matched against a subject string from left to right. The word "Regular expression" is a
6- mouthful, you will usually find the term abbreviated as "regex" or "regexp". Regular expression is used for replacing a text withing
6+ mouthful, you will usually find the term abbreviated as "regex" or "regexp". Regular expression is used for replacing a text within
77a string, validating form, extract a substring from a string based upon a pattern match, and so much more.
88
9- Imagine you are writing an application and you want to set the rules when user chosing their username. We want the username can
9+ Imagine you are writing an application and you want to set the rules when user choosing their username. We want the username can
1010contains letter, number, underscore and hyphen. We also want to limit the number of characters in username so it does not look ugly.
1111We use the following regular expression to validate a username:
1212
@@ -33,7 +33,7 @@ contains uppercase letter and also it is too short.
3333 - [ Escaping special character] ( )
3434 - [ Anchors] ( )
3535 - [ Caret] ( )
36- - [ Dolar ] ( )
36+ - [ Dollar ] ( )
3737- [ Shorthand Character Sets] ( )
3838- [ Lookaround] ( )
3939 - [ Positive Lookahead] ( )
@@ -44,6 +44,7 @@ contains uppercase letter and also it is too short.
4444 - [ Case Insensitive] ( )
4545 - [ Global search] ( )
4646 - [ Multiline] ( )
47+ - [ Bonus] ( )
4748
4849## 1. Basic Matchers
4950
@@ -128,7 +129,7 @@ differently in different situations.
128129### 2.3.1 The Star
129130
130131The symbol ` * ` matches zero or more repetitions of the preceding matcher. The regular expression ` a* ` means: zero or more repetitions
131- of preceding lowercase character ` a ` . But if it apperas after a character set or class that it finds the repetitions of the whole
132+ of preceding lowercase character ` a ` . But if it appears after a character set or class that it finds the repetitions of the whole
132133character set. For example the regular expression ` [a-z]* ` means: any number of lowercase letters in a row.
133134
134135<pre >
@@ -227,13 +228,13 @@ expression `[f|c|m]at\.?` means: lowercase letter `f`, `c` or `m`, followed by l
227228
228229In regular expression to check if the matching symbol is the starting symbol or endnig symbol of the input string for this purpose
229230we use anchors. Anchors are of two types: First type is Caret ` ^ ` that check if the matching character is the start character of the
230- input and the second type is Dolar ` $ ` that checks if matching character is the last character of the input string.
231+ input and the second type is Dollar ` $ ` that checks if matching character is the last character of the input string.
231232
232233### 2.7.1 Caret
233234
234- Caret ` ^ ` symbol is use to check if matching character is the first character of the input string. If we apply the following regular
235+ Caret ` ^ ` symbol is used to check if matching character is the first character of the input string. If we apply the following regular
235236expression ` ^a ` (if a is the starting symbol) to input string ` abc ` it matches ` a ` . But if we apply regular expression ` ^b ` on above
236- input string it does not match anything. Beacause in input string ` abc ` "b" is not the starting symbol. Lets take a look on another
237+ input string it does not match anything. Because in input string ` abc ` "b" is not the starting symbol. Let's take a look on another
237238regular expression ` ^[T|t]he ` which means: uppercase character ` T ` or lowercase character ` t ` is the start symbol of the input string,
238239followed by lowercase character ` h ` , followed by lowercase character ` e ` .
239240
@@ -245,9 +246,9 @@ followed by lowercase character `h`, followed by lowercase character `e`.
245246"^[T|t]he" => <a href =" #learn-regex " ><strong >The</strong ></a > car is parked in the garage.
246247</pre >
247248
248- ### 2.7.2 Dolar
249+ ### 2.7.2 Dollar
249250
250- Dolar ` $ ` symbol is use to check if matching character is the last character of the input string. For example regular expression
251+ Dollar ` $ ` symbol is used to check if matching character is the last character of the input string. For example regular expression
251252` (at.)$ ` means: lowercase character ` a ` , followed by lowercase character ` t ` , followed by anything except new line and the matcher
252253must be end of the string.
253254
@@ -306,7 +307,7 @@ followed by letter `h`, followed by letter `e`. In braces we define positive loo
306307
307308Negative lookahead is used when we need to get all matches from input string that are not followed by a pattern. Negative lookahead
308309defined same as we define positive lookahead but the only difference is instead of equal ` = ` character we use negation ` ! ` character
309- i.e. ` (?!...) ` . Lets take a look at the following regular expression ` [T|t]he(?!\sfat) ` which means: get all ` The ` or ` the ` words from
310+ i.e. ` (?!...) ` . Let's take a look at the following regular expression ` [T|t]he(?!\sfat) ` which means: get all ` The ` or ` the ` words from
310311input string that are not followed by the word ` fat ` precedes by a space character.
311312
312313<pre >
@@ -335,7 +336,7 @@ are after not after the word `The` or `the`.
335336
336337## 5. Flags
337338
338- Flags are also called modifiers beacause they modifies the output of a regular expression. These flags can be used in any order or
339+ Flags are also called modifiers because they modifies the output of a regular expression. These flags can be used in any order or
339340combination, and are an integral part of the RegExp.
340341
341342| Flag| Description|
@@ -348,7 +349,7 @@ combination, and are an integral part of the RegExp.
348349
349350The ` i ` modifier is used to perform case-insensitive matching. For example the regular expression ` /The/gi ` means: uppercase letter
350351` T ` , followed by lowercase character ` h ` , followed by character ` e ` . And at the end of regular expression the ` i ` flag tells the
351- regular expression engine to ignore the case. As you can see we also provided ` g ` flag beacause we want to search for the pattern in
352+ regular expression engine to ignore the case. As you can see we also provided ` g ` flag because we want to search for the pattern in
352353the whole input string.
353354
354355<pre >
@@ -364,7 +365,7 @@ the whole input string.
364365
365366The ` g ` modifier is used to perform a global match (find all matches rather than stopping after the first match). For example the
366367regular expression` /.(at)/g ` means: any character except new line, followed by lowercase character ` a ` , followed by lowercase
367- character ` t ` . Beacause we provided ` g ` flag at the end of the regular expression now it will find every matches from whole input
368+ character ` t ` . Because we provided ` g ` flag at the end of the regular expression now it will find every matches from whole input
368369string.
369370
370371
@@ -379,7 +380,7 @@ string.
379380
380381### 5.3 Multiline
381382
382- The ` m ` modifier is used to perform a multiline match. As we discussed earlier anchors ` (^, $) ` are used to check if pattern is
383+ The ` m ` modifier is used to perform a multi line match. As we discussed earlier anchors ` (^, $) ` are used to check if pattern is
383384the beginning of the input or end fo the input string. But if we want that anchors works on each line we use ` m ` flag. For example the
384385regular expression ` /at(.)?$/gm ` means: lowercase character ` a ` , followed by lowercase character ` t ` , optionally anything except new
385386line. And beacause of ` m ` flag now regular expression engine matches pattern at the end of each line in a string.
@@ -396,6 +397,27 @@ line. And beacause of `m` flag now regular expression engine matches pattern at
396397 on the <a href =" #learn-regex " ><strong >mat.</strong ></a >
397398</pre >
398399
400+ ## Bonus
401+
402+ * * Positive Integers* : ` ^\d+$ `
403+ * * Negative Integers* : ` ^-\d+$ `
404+ * * Phone Number* : ` ^+?[\d\s]{3,}$ `
405+ * * Phone with code* : ` ^+?[\d\s]+(?[\d\s]{10,}$ `
406+ * * Integers* : ` ^-?\d+$ `
407+ * * Username* : ` ^[\w\d_.]{4,16}$ `
408+ * * Alpha-numeric characters* : ` ^[a-zA-Z0-9]*$ `
409+ * * Alpha-numeric characters with spaces* : ` ^[a-zA-Z0-9 ]*$ `
410+ * * Password* : ` ^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$ `
411+ * * email* : ` ^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$ `
412+ * * IP address* : ` ^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$ `
413+ * * Lowercase letters only* : ` ^([a-z])*$ `
414+ * * Uppercase letters only* : ` ^([A-Z])*$ `
415+ * * URL* : ` ^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$ `
416+ * * VISA credit card numbers* : ` ^(4[0-9]{12}(?:[0-9]{3})?)*$ `
417+ * * Date (MM/DD/YYYY)* : ` ^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}$ `
418+ * * Date (YYYY/MM/DD)* : ` ^(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])$ `
419+ * * MasterCard credit card numbers* : ` ^(5[1-5][0-9]{14})*$ `
420+
399421## Contribution
400422
401423* Report issues
0 commit comments