You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Miscellaneous/common_regex.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,27 @@
2
2
3
3
## Matching a Username
4
4
5
-
###/^[a-z0-9_-]{3,16}$/
5
+
>/^[a-z0-9_-]{3,16}$/
6
6
7
7
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 ($)
8
8
9
9
## Matching a Hex Value
10
10
11
-
###/^#?([a-f0-9]{6}|[a-f0-9]{3})$/
11
+
>/^#?([a-f0-9]{6}|[a-f0-9]{3})$/
12
12
13
13
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 ($).
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 ($).
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.
28
28
@@ -33,7 +33,7 @@ Then a trailing slash is matched, but it can be optional. Finally end with the e
33
33
34
34
## Matching an HTML Tag
35
35
36
-
###/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
36
+
>/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
37
37
38
38
It matches any HTML tag with the content inside. As usual, begin with the start of the line.
39
39
@@ -46,4 +46,4 @@ The regex is ended with the end of the line.
0 commit comments