REGULAR EXPRESSION ENHANCEMENTS
ECMAScript 2018 features a handful of new bells and whistles for regular expressions.
dotAll Flag
One annoying quirk of regular expressions was that the single character match token (the period “.”) does not match line terminator characters, such as \n
and \r
, or non-BMP characters, such as emojis.
const text = `
foo
bar
`;
const re = /foo.bar/;
console.log(re.test(text)); // false
This proposal introduces the “s” flag (standing for singleline), which corrects for this behavior:
const text = `
foo
bar
`;
const re = /foo.bar/s;
console.log(re.test(text)); // true
Lookbehind Assertions
Regular expressions support both positive and negative lookahead assertions, which allow declaration of expectations following a matched segment:
const text = 'foobar';
// Positive lookahead
// Assert that a value follows, but do not capture
const rePositiveMatch = /foo(?=bar)/;
const rePositiveNoMatch = /foo(?=baz)/;
console...