Guided learning15 minutes · Beginner

From RegEx to STRling

Keep the RegEx concepts you already know and express them through readable Pattern constructors instead of dense punctuation.

The examples use the TypeScript 4.0.3 Preview adapter. Start with the Core concepts setup.

Translate intent before syntax

Identify the input domain, boundaries, and captures you intend. Recreate that intent with Simply operations and compile for an explicit target profile. The table describes source intent. Inspect the artifact for the target’s actual spelling and flags.

RegEx intentTypeScript Simply
Literal texts.lit('report.pdf')
Sequences.merge(a, b)
Alternativess.anyOf(a, b)
Optional sequences.may(a, b)
Explicit ASCII digitss.between('0', '9')
Two to four occurrencespiece.rep(2, 4)
Named captures.group('serial', piece)
Positive lookaheads.ahead(piece)
Strict input boundariess.start(), s.end()

Check the translated pattern

The expression accepts a literal label, an optional hyphen, and two through four ASCII digits. The capture contains only the digits. The host RegEx engine performs the match.

const serial = s.between('0', '9').rep(2, 4);
const pattern = s.merge(
  s.start(),
  'ID',
  s.may('-'),
  s.group('serial', serial),
  s.end(),
);
const expression = compilePattern(pattern);
console.log(expression.exec('ID-042')?.groups?.serial); // 042
console.log(expression.test('ID-042')); // false
console.log(expression.test('ID-042\n')); // false

Use a character set for one position and alternatives for whole patterns. Keep lookaround width, Unicode classes, and matching mode in the portability review. To import raw source programmatically, use the versioned regex compatibility frontend and its documented accepted subset. See Core concepts for the distinction between that import dialect and the Semantic STRling textual language.