Regular Expressions For Regular Folk

Advanced Examples

Javascript comments

/\/\*[\s\S]*?\*\/|\/\/.*/g[RegExr] [Visual]
  • 1 matchconst a = 0; // comment
    1. // comment
  • 1 match/* multiline */
    1. /* 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 match23:59:00
    1. 23:59:00
  • 1 match14:00
    1. 14:00
  • 1 match23:00
    1. 23:00
  • 0 matches29:00
    1. 0 matches32:32

      Meta

      /<Example source="(.*?)" flags="(.*?)">/gm[RegExr] [Visual]
      • 1 match<Example source="p[aeiou]t" flags="g">
        1. <Example source="p[aeiou]t" flags="g">
      • 1 match<Example source="s+$" flags="gm">
        1. <Example source="s+$" flags="gm">
      • 1 match<Example source="(['"])(?:(?!\1).)*\1" flags="g">
        1. <Example source="(['"])(?:(?!\1).)*\1" flags="g">
      • 0 matches<Example source='s+$' flags='gm'>
        1. 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 match987
            1. 987
          • 1 match-8
            1. -8
          • 1 match0.1
            1. 0.1
          • 1 match2.
            1. 2.
          • 1 match.987
            1. .987
          • 1 match+4.0
            1. +4.0
          • 1 match1.1e+1
            1. 1.1e+1
          • 1 match1.e+1
            1. 1.e+1
          • 1 match1e2
            1. 1e2
          • 1 match0.2e2
            1. 0.2e2
          • 1 match.987e2
            1. .987e2
          • 1 match+4e-1
            1. +4e-1
          • 1 match-8.e+2
            1. -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 match30.0260736, -89.9766792
              1. 30.0260736, -89.9766792
            • 1 match45, 180
              1. 45, 180
            • 1 match-90.000, -180.0
              1. -90.000, -180.0
            • 1 match48.858093,2.294694
              1. 48.858093,2.294694
            • 1 match-3.14, 3.14
              1. -3.14, 3.14
            • 1 match045, 180.0
              1. 045, 180.0
            • 1 match0, 0
              1. 0, 0
            • 0 matches-90., -180.
              1. 0 matches.004, .15

                See also: Floating Point Numbers

                MAC Addresses

                /^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/i[RegExr] [Visual]
                • 1 match01:02:03:04:ab:cd
                  1. 01:02:03:04:ab:cd
                • 1 match9E:39:23:85:D8:C2
                  1. 9E:39:23:85:D8:C2
                • 1 match00:00:00:00:00:00
                  1. 00:00:00:00:00:00
                • 0 matches1N:VA:L1:DA:DD:R5
                  1. 0 matches9:3:23:85:D8:C2
                    1. 0 matchesac::23:85:D8:C2

                      UUID

                      /[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}/i[RegExr] [Visual]
                      • 1 match123e4567-e89b-12d3-a456-426655440000
                        1. 123e4567-e89b-12d3-a456-426655440000
                      • 1 matchc73bcdcc-2669-4bf6-81d3-e4ae73fb11fd
                        1. c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd
                      • 1 matchC73BCDCC-2669-4Bf6-81d3-E4AE73FB11FD
                        1. C73BCDCC-2669-4Bf6-81d3-E4AE73FB11FD
                      • 0 matchesc73bcdcc-2669-4bf6-81d3-e4an73fb11fd
                        1. 0 matchesc73bcdcc26694bf681d3e4ae73fb11fd

                          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 match9.9.9.9
                            1. 9.9.9.9
                          • 1 match127.0.0.1:8080
                            1. 127.0.0.1
                          • 1 matchIt's 192.168.1.9
                            1. 192.168.1.9
                          • 1 match255.193.09.243
                            1. 255.193.09.243
                          • 1 match123.123.123.123
                            1. 123.123.123.123
                          • 0 matches123.123.123.256
                            1. 0 matches0.0.x.0

                              HSL colours

                              Integers from 0 to 360

                              • 360
                              • 300 to 3593, [0-5], any digit
                              • 0 to 299
                                • optionally 1 or 2 as the hundreds digit
                                • optionally any tens digit
                                • a units digit
                              /^0*(?:360|3[0-5]\d|[12]?\d?\d)$/g[RegExr] [Visual]
                              • 1 match360
                                1. 360
                              • 1 match349
                                1. 349
                              • 1 match235
                                1. 235
                              • 1 match152
                                1. 152
                              • 1 match68
                                1. 68
                              • 1 match9
                                1. 9
                              • 0 matches361
                                1. 0 matches404

                                  Percentages

                                  • 100, optionally followed by .000…
                                  • one or two digit integer, optionally followed decimal part
                                  /^(?:100(?:\.0+)?|\d?\d(?:\.\d+)?)%$/g[RegExr] [Visual]
                                  • 1 match100%
                                    1. 100%
                                  • 1 match100.0%
                                    1. 100.0%
                                  • 1 match25%
                                    1. 25%
                                  • 1 match52.32%
                                    1. 52.32%
                                  • 1 match9%
                                    1. 9%
                                  • 1 match0.5%
                                    1. 0.5%
                                  • 0 matches100.5%
                                    1. 0 matches42

                                      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 matchhsl(0,20%,100%)
                                        1. hsl(0,20%,100%)
                                      • 1 matchHSL(0350, 002%,4.1%)
                                        1. HSL(0350, 002%,4.1%)
                                      • 1 matchhsl(360,10% , 0.2% )
                                        1. hsl(360,10% , 0.2% )