Advanced Examples
Javascript comments
/\/\*[\s\S]*?\*\/|\/\/.*/g[RegExr] [Visual]- 1 match
const a = 0; // comment// comment
- 1 match
/* multiline *//* multiline */
[\s\S] is a hack to match any character including newlines. We avoid the dot-all flag because we need to use the ordinary . for single-line comments.
24-Hour Time
/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/g[RegExr] [Visual]- 1 match
23:59:0023:59:00
- 1 match
14:0014:00
- 1 match
23:0023:00
- 0 matches
29:00 - 0 matches
32:32
Meta
/<Example source="(.*?)" flags="(.*?)">/gm[RegExr] [Visual]- 1 match
<Example source="p[aeiou]t" flags="g"><Example source="p[aeiou]t" flags="g">
- 1 match
<Example source="s+$" flags="gm"><Example source="s+$" flags="gm">
- 1 match
<Example source="(['"])(?:(?!\1).)*\1" flags="g"><Example source="(['"])(?:(?!\1).)*\1" flags="g">
- 0 matches
<Example source='s+$' flags='gm'> - 0 matches
</Example>
Replace: <Example regex={/$1/$2}>
I performed this operation in commit d7a684f.
Floating point numbers
- optional sign
- optional integer part
- optional decimal part
- optional exponent part
/^([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?$/g[RegExr] [Visual]- 1 match
987987
- 1 match
-8-8
- 1 match
0.10.1
- 1 match
2.2.
- 1 match
.987.987
- 1 match
+4.0+4.0
- 1 match
1.1e+11.1e+1
- 1 match
1.e+11.e+1
- 1 match
1e21e2
- 1 match
0.2e20.2e2
- 1 match
.987e2.987e2
- 1 match
+4e-1+4e-1
- 1 match
-8.e+2-8.e+2
- 0 matches
.
The positive lookahead (?=\.\d|\d) ensures that the regex does not match ..
Latitude and Longitude
/^((-?|\+?)?\d+(\.\d+)?),\s*((-?|\+?)?\d+(\.\d+)?)$/g[RegExr] [Visual]- 1 match
30.0260736, -89.976679230.0260736, -89.9766792
- 1 match
45, 18045, 180
- 1 match
-90.000, -180.0-90.000, -180.0
- 1 match
48.858093,2.29469448.858093,2.294694
- 1 match
-3.14, 3.14-3.14, 3.14
- 1 match
045, 180.0045, 180.0
- 1 match
0, 00, 0
- 0 matches
-90., -180. - 0 matches
.004, .15
See also: Floating Point Numbers
MAC Addresses
/^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/i[RegExr] [Visual]- 1 match
01:02:03:04:ab:cd01:02:03:04:ab:cd
- 1 match
9E:39:23:85:D8:C29E:39:23:85:D8:C2
- 1 match
00:00:00:00:00:0000:00:00:00:00:00
- 0 matches
1N:VA:L1:DA:DD:R5 - 0 matches
9:3:23:85:D8:C2 - 0 matches
ac::23:85:D8:C2
UUID
/[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}/i[RegExr] [Visual]- 1 match
123e4567-e89b-12d3-a456-426655440000123e4567-e89b-12d3-a456-426655440000
- 1 match
c73bcdcc-2669-4bf6-81d3-e4ae73fb11fdc73bcdcc-2669-4bf6-81d3-e4ae73fb11fd
- 1 match
C73BCDCC-2669-4Bf6-81d3-E4AE73FB11FDC73BCDCC-2669-4Bf6-81d3-E4AE73FB11FD
- 0 matches
c73bcdcc-2669-4bf6-81d3-e4an73fb11fd - 0 matches
c73bcdcc26694bf681d3e4ae73fb11fd
IP Addresses
/\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/g[RegExr] [Visual]- 1 match
9.9.9.99.9.9.9
- 1 match
127.0.0.1:8080127.0.0.1
- 1 match
It's 192.168.1.9192.168.1.9
- 1 match
255.193.09.243255.193.09.243
- 1 match
123.123.123.123123.123.123.123
- 0 matches
123.123.123.256 - 0 matches
0.0.x.0
HSL colours
Integers from 0 to 360
360300to359—3,[0-5], any digit0to299- optionally
1or2as the hundreds digit - optionally any tens digit
- a units digit
- optionally
/^0*(?:360|3[0-5]\d|[12]?\d?\d)$/g[RegExr] [Visual]- 1 match
360360
- 1 match
349349
- 1 match
235235
- 1 match
152152
- 1 match
6868
- 1 match
99
- 0 matches
361 - 0 matches
404
Percentages
100, optionally followed by.000…- one or two digit integer, optionally followed decimal part
/^(?:100(?:\.0+)?|\d?\d(?:\.\d+)?)%$/g[RegExr] [Visual]- 1 match
100%100%
- 1 match
100.0%100.0%
- 1 match
25%25%
- 1 match
52.32%52.32%
- 1 match
9%9%
- 1 match
0.5%0.5%
- 0 matches
100.5% - 0 matches
42
Bringing it all together
/^hsl\(\s*0*(?:360|3[0-5]\d|[12]?\d?\d)\s*(?:,\s*0*(?:100(?:\.0+)?|\d?\d(?:\.\d+)?)%\s*){2}\)$/gi[RegExr] [Visual]- 1 match
hsl(0,20%,100%)hsl(0,20%,100%)
- 1 match
HSL(0350, 002%,4.1%)HSL(0350, 002%,4.1%)
- 1 match
hsl(360,10% , 0.2% )hsl(360,10% , 0.2% )