Skip to content

Commit 02a069f

Browse files
authored
Update common_regex.md
1 parent 3c15b78 commit 02a069f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Miscellaneous/common_regex.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
## Matching a Username
44

5-
### /^[a-z0-9_-]{3,16}$/
5+
> /^[a-z0-9_-]{3,16}$/
66
77
The beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} makes sure that are at least 3 of those characters, but no more than 16. Finally, the end of the string ($)
88

99
## Matching a Hex Value
1010

11-
### /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
11+
> /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
1212
1313
The beginning of the string (^). Next, a (#) is optional because it is followed by a (?). The question mark signifies that the preceding character is optional, but to capture it if it's there. Next, inside the first group (first group of parentheses), we can have two different situations. The first is any lowercase letter between a and f or a number six times. The vertical bar tells us that we can also have three lowercase letters between a and f or numbers instead. Finally, we want the end of the string ($).
1414

1515

1616
## Matching an Email
1717

18-
### /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
18+
> /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
1919
2020
The beginning of the string (^). Inside the first group, match one or more lowercase letters, numbers, underscores, dots, or hyphens. The dot is escaped because a non-escaped dot means any character. Directly after that, there must be an at sign. Next is the domain name which must be: one or more lowercase letters, numbers, underscores, dots, or hyphens. Then another (escaped) dot, with the extension being two to six letters or dots. There are 2 to 6 because of the country specific TLD's (.com or .co.in). Finally, we want the end of the string ($).
2121

2222

2323
## Matching a URL
2424

25-
### /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
25+
> /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
2626
2727
The first capturing group is all option. It allows the URL to begin with "http://", "https://", or neither of them. A question mark after the s allows URL's that have http or https. In order to make this entire group optional, a question mark is added to the end of it.
2828

@@ -33,7 +33,7 @@ Then a trailing slash is matched, but it can be optional. Finally end with the e
3333

3434
## Matching an HTML Tag
3535

36-
### /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
36+
> /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
3737
3838
It matches any HTML tag with the content inside. As usual, begin with the start of the line.
3939

@@ -46,4 +46,4 @@ The regex is ended with the end of the line.
4646

4747
## Matching an IP Address
4848

49-
### /^(?:(?: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]?)$/
49+
> /^(?:(?: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]?)$/

0 commit comments

Comments
 (0)