Javascript Regular Expressions

During my time as a developer I have always shied away from regular expressions. Just looking at the syntax was off-putting so I placed the topic firmly on the “get to it later” list. Every now and then while working on a project I came across a situation where regular expressions were necessary. Usually in those situations Stack Overflow was my savior. However this is not an acceptable solution so I decided to finally dive into regular expressions and explore exactly what they are and what types of problems they can solve. So, to start, what exactly are regular expressions?

What are Regular Expressions?

According to Wikipedia regular expressions (also commonly known as regex and regexp) are “a sequence of characters that define a search pattern.” Usually these patterns are used by string-searching algorithms for operations on strings such as find or find and replace, or for input validation.

So with that in mind, what are some applications of regular expressions? Regular expressions can be used in search engines and search and replace features in word processors/text editors, among tons of other applications. Many programming languages provide regular expression capabilities sometimes natively or by a 3rd party library.

Creating a New Regular Expression

There are 2 ways to create a regular expression in Javascript, a regular expression constructor or a regular expression literal. I personally used to seeing them written as literals as opposed to as a constructor. However both syntaxes are valid and you may seem them written either way.

As a constructor:

The syntax here is new RegExp(pattern)

As a literal:

The syntax here is /pattern/

Both of these syntaxes also accept flags, which we’ll discuss further later on.

Methods

Now is a good time to discuss methods for regular expressions. For a full list of regular expressions, see this MDN guide. These methods include exec, match, matchAll, replace, replaceAll, search, and test. For now I’ll just go over the test method. This method returns a boolean (true or false) depending on if a match is found or not.

Flags

Until now our regular expressions have been quite simple as we have been omitting flags. However the use of flags will really unlock regular expressions’ power. Flags allow us to do this. There are 6 flags that can be used with regular expressions:

  • g → global search
  • i → case sensitive search
  • m → multi-line search
  • s → allows . to match newline characters
  • u → “unicode”; treat a pattern as a sequence of unicode code points
  • y → Perform a sticky search that matches starting at the current position in the target string

The g and i flags I believe are what you’ll come across the most. To update our syntax from above with flags:

As a constructor:

The syntax here is new RegExp(pattern, 'flags')

As a literal:

The syntax here is /pattern/flags

This is a brief introduction and to JS regular expressions. If you are looking for more tools and resources for regular expressions see below: