Regular Expressions For Regular Folk

Basics

Regular expressions are typically formatted as /<rules>/<flags>. Often people will drop the slashes and flags for brevity. We’ll get into the details of flags in a later chapter.

Let’s start with the regex /p/g. For now, please take the g flag for granted.

/p/g[RegExr] [Visual]
  • 1 matchpancake
    1. p
  • 3 matchespineapple
    1. p
    2. p
    3. p
  • 2 matchesapple
    1. p
    2. p
  • 0 matchesmango
    1. 0 matchesPlum

      As we can see, /p/g matches all lowercase p characters.

      Note

      Regular expressions are case-sensitive by default.

      Instances of the regex pattern found in an input string are termed “matches”.

      /pp/g[RegExr] [Visual]
      • 1 matchapple
        1. pp
      • 1 matchpineapple
        1. pp
      • 1 matchhappiness
        1. pp
      • 2 matchessipping apple juice
        1. pp
        2. pp
      • 0 matchespapaya