Codectionary / Developer documentation / JavaScript

Named Captures and Unicode Regular Expressions

Named capture groups expose matched portions through a groups object rather than positional indexes. Unicode property escapes such as \p{L} match Unicode character categories with the u flag. Both features were added in ES2018; they are useful when ASCII-only classes would reject ordinary international text.

Syntax

/^(?<name>\p{L}+)=(?<count>\d+)$/u

Examples

Named Captures and Unicode Regular Expressions: working example

Run this standalone example in a modern browser console unless another environment is specified. Expected result: The console prints café 13, then false.

const pattern = /^(?<name>\p{L}+)=(?<count>\d+)$/u;
const match = pattern.exec("café=12");
if (match) {
  console.log(match.groups.name, Number(match.groups.count) + 1);
}
console.log(pattern.test("tea=-2"));
  1. Expected result: The console prints café 13, then false.
  2. Named groups keep extraction readable when the pattern grows.
  3. The anchors require a complete match, and the unsigned decimal group rejects -2.

Best practices

  • Check for a null match before reading captures.
  • Define validation rules for the actual domain rather than assuming letters-only works for all names.

Runtime & versions

Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ECMAScript specification

Specifications & further reading