Unlock ES2018 Regex: Lookbehind, Named Groups, DotAll & Unicode Made Simple
This article explains the four new ES2018 regular‑expression features—lookbehind assertions, named capturing groups, the dotAll (s) flag, and Unicode property classes—showing how they work, when to use them, and providing clear JavaScript code examples.
Overview
If you have ever performed complex text processing with JavaScript, the ES2018 update brings four powerful regex enhancements that make such tasks easier and more expressive.
Lookbehind assertions
Named capturing groups
DotAll s flag
Unicode property classes
Lookahead and Lookbehind
Before ES2018 JavaScript only supported lookahead assertions ( (?=...) and (?!...)). ES2018 adds lookbehind assertions, written as (?<=...) for positive and (?<!...) for negative lookbehind.
const re = /(?<=€)\d+(\.\d*)?/;
console.log(re.exec('199')); // → null (no preceding €)
console.log(re.exec('€199')); // → ["199", undefined, index: 1, input: "€199", groups: undefined]Multiple lookbehinds can be chained to create more complex patterns, e.g. matching a number of two digits that is not 35:
const re = /(?<=\d{2})(?<!35) meters/;
console.log(re.exec('35 meters')); // → null
console.log(re.exec('14 meters')); // → [" meters", index: 2, input: "14 meters", groups: undefined]Named Capturing Groups
Named groups use the syntax (?<name>...). The match result contains a groups object where each property is the group name.
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = re.exec('2020-03-04');
console.log(match.groups); // {year: "2020", month: "03", day: "04"}If a named group does not participate in the match, its value is undefined.
DotAll s Flag
Normally the dot ( .) matches any character except line terminators ( \n and \r). Adding the s flag makes the dot match line terminators as well.
console.log(/./.test('
')); // false
console.log(/./s.test('
')); // trueThe flag can be combined with any regex without altering previous behaviour.
Unicode Property Classes
With the u flag, Unicode property escapes \p{...} and \P{...} allow matching characters by their Unicode properties. For example, to match any Unicode digit:
const re = /\p{Number}/u;
console.log(re.test('𝟠')); // trueTo match any alphabetic character:
const re = /\p{Alphabetic}/u;
console.log(re.test('漢')); // trueNegated property classes use \P{...} and match characters that do not have the specified property.
Compatibility
Browser support for these features varies; modern browsers and recent Node.js versions implement them fully. A compatibility table is often shown in documentation.
Summary
ES2018 extends JavaScript regular expressions with lookbehind assertions, named capturing groups, the dotAll s flag, and Unicode property classes. These additions make patterns more readable and powerful, especially for complex text processing tasks.
When writing intricate regexes, using a testing tool such as Regex101 or RegexBuddy helps visualise matches, debug syntax errors, and understand patterns written by others.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
