Position Anchors

AnchorMatches position at
^ start of the string
$ end of the string
\b boundary of word (start or end)
\B all positions not \b

Empty-string match: repetition operators not applicable.

Bracket Sets

NotationSampleMatches
Direct concatenation [aeiouy] lowercase vowels
Range (hyphen) [A-M] uppercase A to M
Range concatenation [A-Za-z0-9] any alphanumerical
Complement (caret) [^0-9] any non-numerical

Meta-character Sets

Meta Matches Any Equivalent
. (dot) not new-line [^\n]
\d digit [0-9]
\w alphanum. or underscore [a-zA-Z0-9_]
\s white-space [\n\r\t\f]

Uppercase meta-chars. match the complement set instead.
For example, \s matches non-white-space characters, which is equivalent to the bracket [^\n\r\t\f].

Occurrence Indicators / Repetition Operators

Indicator Matches
? either 0 or 1
* 0 or more
+ 1 or more
{M,} M or more
{M} exactly M
{M,N} M to N, both inclusive

Suffixes changing the quantity of the prior.
For example, a{4} is equivalent to aaaa.

Common Flags / Modes

Flag Name Change
gglobal All matches returned (vs. first)
mmulti-line ^/$ by line (vs. entire string)
icase-insensitive Letters turn case-insensitive
sdot-all Dot also matches \n

The listed flags are those available in most Regex engines. They are used to change the behavior of the matching algorithm. Each implementation has a specific way to set the flags. In Javascript, in RegExp constructor or suffix of forward-slash notation, In Python re, provided as optional argument to callables. In Regex commands, as regular flag.

Capture Groups

Parenthesis are used to mark capture groups. By index, starting at 1, they can be referenced in the replacements. Different notations exist, depending on the tool.

Regex in Visual Studio Code

Most HTML pages of this site are made by hand, which is enabled by a frequent use of Regex Find-and-Replace. It is faster than manual editing for repetitive patterns. It does not require a new file, unlike scripts. It provides un-doing and counts, unlike command pipelines. It is also possible to target multiple files with a first-step globbing pattern, and a second-step Regex pattern for the text operation.

    Search
      <address (.*)</address>
      <time (.*)</time>
    Replace
      <time $2</time>
      <address $1</address>
  

The above swaps two elements in-place. It includes their content, plus their attributes by not closing the start-tag. To write correct patterns for the needed job with confidence, it helps to pair hand-made, known mark-up, with good knowledge of HTML specification. In this case, it is known the content of both has no new-lines, which makes dot-star sufficient.

Regex pattern to validate input

This example pattern='[0-9abcdefABCDEF]{6}' validates the input values, rejecting strings that are not hexadecimal values of length six / 24 bits. It also marks it required, otherwise length zero is also accepted despite the mismatching pattern.

Working with Regex in JavaScript.

Regular Expressions in Javascript can be instantiated either via the RegExp constructor, or directly by using forward-slash instead of regular quotation. Both have different mechanisms for flagging, the snippet showing this for the global flag (a requirement for findAll and replaceAll operations).

    const by_ctor = new RegExp(".*");
    const by_fwds = /.*/;

    const by_ctor_global = new RegExp(".*", 'g');
    const by_fwds_global = /.*/g;
  

Interactive Demo

This text sample is editable.

ABCDEF |  abcdef
GHIJKL | g hijkl
MNOPQR | mn opqr
STUVWX | stu vwx
YZ0123 | yz01 23
456789 | 456789

abc def ghi jkl mno pqr
stu vwx yz0 123 456 789

  Lorem ipsum dolor sit amet, 
  consectetur adipiscing elit.
  Nulla massa turpis,
  pretium sit amet erat vitae, 
  porta suscipit tellus.
  Fusce accumsan elit eu justo vehicula, 
  quis tempus nulla condimentum.
  Maecenas maximus, dolor nec fermentum eleifend, 
  nisi enim bibendum mauris, 
  ut consequat justo tortor ut enim.