Advanced Examples
Javascript comments
/\/\*[\s\S]*?\*\/|\/\/.*/g- 1 match
const a = 0; // comment - 1 match
/* 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- 1 match
23:59:00 - 1 match
14:00 - 1 match
23:00 - 0 matches
29:00 - 0 matches
32:32
Meta
/<Example source="(.*?)" flags="(.*?)">/gm- 1 match
<Example source="p[aeiou]t" flags="g"> - 1 match
<Example source="s+$" flags="gm"> - 1 match
<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- 1 match
987 - 1 match
-8 - 1 match
0.1 - 1 match
2. - 1 match
.987 - 1 match
+4.0 - 1 match
1.1e+1 - 1 match
1.e+1 - 1 match
1e2 - 1 match
0.2e2 - 1 match
.987e2 - 1 match
+4e-1 - 1 match
-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- 1 match
30.0260736, -89.9766792 - 1 match
45, 180 - 1 match
-90.000, -180.0 - 1 match
48.858093,2.294694 - 1 match
-3.14, 3.14 - 1 match
045, 180.0 - 1 match
0, 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- 1 match
01:02:03:04:ab:cd - 1 match
9E:39:23:85:D8:C2 - 1 match
00: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- 1 match
123e4567-e89b-12d3-a456-426655440000 - 1 match
c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd - 1 match
C73BCDCC-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- 1 match
9.9.9.9 - 1 match
127.0.0.1:8080 - 1 match
It's 192.168.1.9 - 1 match
255.193.09.243 - 1 match
123.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- 1 match
360 - 1 match
349 - 1 match
235 - 1 match
152 - 1 match
68 - 1 match
9 - 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- 1 match
100% - 1 match
100.0% - 1 match
25% - 1 match
52.32% - 1 match
9% - 1 match
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- 1 match
hsl(0,20%,100%) - 1 match
HSL(0350, 002%,4.1%) - 1 match
hsl(360,10% , 0.2% )