11# Regex Examples
22
3- ## JavaScript number
3+ ## Match hashtags
4+
5+ This regex matches and captures all hashtags in a given string.
6+
7+ ``` ts
8+ const hashtags = buildRegExp (
9+ [
10+ ' #' ,
11+ capture (oneOrMore (word )),
12+ ],
13+ { global: true },
14+ );
15+
16+ const hashtagMatches = ' #hello #world' .matchAll (hashtags );
17+ ```
18+
19+ Encoded regex: ` /#(\w+)/g ` .
20+
21+ See tests: [ example-hashtags.ts] ( ../src/__tests__/example-hashtags.ts ) .
22+
23+ ## Hex color validation
24+
25+ This regex validate whether given string is a valid hex color, with 6 or 3 hex digits.
26+
27+ ``` ts
28+ const hexDigit = charClass (digit , charRange (' a' , ' f' ));
29+
30+ const regex = buildRegExp (
31+ [
32+ startOfString ,
33+ optional (' #' ),
34+ choiceOf (
35+ repeat (hexDigit , 6 ), // #rrggbb
36+ repeat (hexDigit , 3 ), // #rgb
37+ ),
38+ endOfString ,
39+ ],
40+ { ignoreCase: true },
41+ );
42+ ```
43+
44+ Encoded regex: ` /^#?(?:[a-f\d]{6}|[a-f\d]{3})$/i ` .
45+
46+ See tests: [ example-hex-color.ts] ( ../src/__tests__/example-hex-color.ts ) .
47+
48+ ## Simple URL validation
49+
50+ This regex validates (in simplified way) whether given string is a URL.
51+
52+ ``` ts
53+ const protocol = [choiceOf (' http' , ' https' ), ' ://' ];
54+ const domainChars = charClass (charRange (' a' , ' z' ), digit );
55+ const domainCharsHypen = charClass (domainChars , anyOf (' -' ));
56+
57+ const domainSegment = choiceOf (
58+ domainChars , // single char
59+ [domainChars , zeroOrMore (domainCharsHypen ), domainChars ], // multi char
60+ );
61+
62+ const regex = buildRegExp ([
63+ startOfString ,
64+ optional (protocol ),
65+ oneOrMore ([domainSegment , ' .' ]), // domain segment
66+ charRange (' a' , ' z' ), // TLD first char
67+ oneOrMore (domainChars ), // TLD remaining chars
68+ endOfString ,
69+ ]);
70+ ```
71+
72+ Encoded regex: ` /^(?:(?:http|https):\/\/)?(?:(?:[a-z\d]|[a-z\d][a-z\d-]*[a-z\d])\.)+[a-z][a-z\d]+$/ ` .
73+
74+ See tests: [ example-url.ts] ( ../src/__tests__/example-url.ts ) .
75+
76+ ## Email address validation
77+
78+ This regex validates whether given string is a properly formatted email address.
79+
80+ ``` ts
81+ const hostnameChars = charClass (charRange (' a' , ' z' ), digit , anyOf (' -.' ));
82+ const domainChars = charRange (' a' , ' z' );
83+
84+ const regex = buildRegExp (
85+ [
86+ startOfString ,
87+ oneOrMore (usernameChars ),
88+ ' @' ,
89+ oneOrMore (hostnameChars ),
90+ ' .' ,
91+ repeat (domainChars , { min: 2 }),
92+ endOfString ,
93+ ],
94+ { ignoreCase: true },
95+ );
96+
97+ const isValid = regex .test (" user@example.com" );
98+ ```
99+
100+ Encoded regex: ` /^[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,}$/i ` .
101+
102+ See tests: [ example-email.ts] ( ../src/__tests__/example-email.ts ) .
103+
104+ ## JavaScript number validation
105+
106+ This regex validates if given string is a valid JavaScript number.
107+
4108
5109``` ts
6110const sign = anyOf (' +-' );
7111const exponent = [anyOf (' eE' ), optional (sign ), oneOrMore (digit )];
8112
9113const regex = buildRegExp ([
114+ startOfString ,
10115 optional (sing ),
11116 choiceOf (
12117 [oneOrMore (digit ), optional ([' .' , zeroOrMore (digit )])], // leading digit
13118 [' .' , oneOrMore (digit )], // leading dot
14119 ),
15120 optional (exponent ), // exponent
121+ endOfString ,
16122]);
123+
124+ const isValid = regex .test (" 1.0e+27" );
17125```
18126
127+ Encoded regex: ` /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/ ` .
128+
129+ See tests: [ example-js-number.ts] ( ../src/__tests__/example-js-number.ts ) .
130+
19131## IPv4 address validation
20132
21133``` ts
@@ -37,9 +149,6 @@ const regex = buildRegExp([
37149]);
38150```
39151
40- This code generates the following regex pattern:
152+ Encoded regex: ` /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/ ` .
41153
42- ``` ts
43- const regex =
44- / ^ (?:(?:\d | [1-9 ] \d | 1\d {2} | 2[0-4 ] \d | 25[0-5 ] )\. ){3} (?:\d | [1-9 ] \d | 1\d {2} | 2[0-4 ] \d | 25[0-5 ] )$ / ;
45- ```
154+ See tests: [ example-ipv4.ts] ( ../src/__tests__/example-ipv4.ts ) .
0 commit comments