Regular Expressions 101

Community Patterns

NextJS Compiler - reactRemoveProperties

0

Regular Expression
PCRE (PHP <7.3)

/
^data-(test|cy|jest).*$
/
gm

Description

Match Test Properties

This regular expression is used on next.config.js and is used to remove test attributes from the production build.

It filters any data attributes that starts with 'test', 'cy' or 'jest'.

Match examples

Using 'test' data attributes

Inputs: <span data-test="test1">SPAN 1</span> <span data-testid="test1">SPAN 2</span> <span data-test-id="test1">SPAN 3</span>

Outputs: <span>SPAN 1</span> <span>SPAN 2</span> <span>SPAN 3</span>

Using 'jest' data attributes

Inputs: <span data-jest="test1">SPAN 1</span> <span data-jestid="test1">SPAN 2</span> <span data-jest-id="test1">SPAN 3</span>

Outputs: <span>SPAN 1</span> <span>SPAN 2</span> <span>SPAN 3</span>

Using 'cy' data attributes

Inputs: <span data-cy="test1">SPAN 1</span> <span data-cyid="test1">SPAN 2</span> <span data-cy-id="test1">SPAN 3</span>

Outputs: <span>SPAN 1</span> <span>SPAN 2</span> <span>SPAN 3</span>

Usage

module.exports = {
  compiler: {
    // The regexes defined here are processed in Rust so the syntax is different from
    // JavaScript `RegExp`s. See https://docs.rs/regex.
    reactRemoveProperties: { properties: ['^data-(test|cy|jest).*$'] },
  },
}
Submitted by Patrick Fantato <patrick@madebyon.com> - 2 years ago