Regular Expressions

Regular Expressions or "regex" is used to describe a series of characters. If it has a pattern in its formatting, regex can be used.

Regular Expression Syntax

This syntax is for Javascript, but it is mostly what I have used in everything so far.

Metacharacters

Shorthand Character Classes

Lookahead and Lookbehind

Lookahead and lookbehind allow search queries that will not be included in the final match. e.g. x(?=y) will return x only if succeeded by y.

Lookahead

Lookbehind

Flags/Suffixes

Backreferences

\1, where 1 is any incremental number, is called a backreference. The number is referring to the capture group of the current reg ex. When called, the regex will search for the exact instance found of that regex.

For example, /[A-Z][0-9]\1/ will search for any char between A-Z, 0-9, and whatever character was found in the first character group. If you were searching through 'A9A' it would be a success, while 'A9B' would fail. This is because capture group 1 found an 'A', and therefore the backreference (\1) must also find an 'A'.

Common Regex Queries

Regex Matches
[\s\S] Anything
[^\n\r] Anything except for a newline
[A-Za-z0-9] Alphanumeric characters
[A-Za-z] Alphabetic characters
[0-9] or \d Numeric characters
[\.,-\/#!$%\^&\*;:{}=\-_`~()@\+\?><\[\]\+] All punctuation (non alphanum graphic characters)

References:

  1. https://regular-expressions.mobi/backref.html?wlr=1
  2. https://regexr.com/
  3. https://regexcrossword.com/

Last modified: 202107020456