This chapter explains new regular expression features in ECMAScript 6. It helps if you are familiar with ES5 regular expression features and Unicode. Consult the following two chapters of “Speaking JavaScript” if necessary:
/y
(sticky)
RegExp.prototype.exec(str)
RegExp.prototype.test(str)
String.prototype.search(regex)
String.prototype.match(regex)
String.prototype.split(separator, limit)
String.prototype.replace(search, replacement)
/u
(unicode)
.
) matches code points, not code unitsflags
RegExp()
can be used as a copy constructor
exec()
The following regular expression features are new in ECMAScript 6:
/y
(sticky) anchors each match of a regular expression to the end of the previous match./u
(unicode) handles surrogate pairs (such as \uD83D\uDE80
) as code points and lets you use Unicode code point escapes (such as \u{1F680}
) in regular expressions.flags
gives you access to the flags of a regular expression, just like source
already gives you access to the pattern in ES5:
RegExp()
to make a copy of a regular expression:
/y
(sticky) The new flag /y
changes two things while matching a regular expression re
against a string:
re.lastIndex
: The match must start at re.lastIndex
(the index after the previous match). This behavior is similar to the ^
anchor, but with that anchor, matches must always start at index 0.re.lastIndex
is set to the index after the match. This behavior is similar to the /g
flag. Like /g
, /y
is normally used to match multiple times.The main use case for this matching behavior is tokenizing, where you want each match to immediately follow its predecessor. An example of tokenizing via a sticky regular expression and exec()
is given later.
Let’s look at how various regular expression operations react to the /y
flag. The following tables give an overview. I’ll provide more details afterwards.
Methods of regular expressions (re
is the regular expression that a method is invoked on):
Flags | Start matching | Anchored to | Result if match | No match | re.lastIndex | |
---|---|---|---|---|---|---|
exec() | – | 0 | – | Match object | null | unchanged |
/g | re.lastIndex |
– | Match object | null | index after match | |
/y | re.lastIndex |
re.lastIndex |
Match object | null | index after match | |
/gy | re.lastIndex |
re.lastIndex |
Match object | null | index after match | |
test() | (Any) | (like exec()) | (like exec()) | true | false | (like exec()) |
Methods of strings (str
is the string that a method is invoked on, r
is the regular expression parameter):
Flags | Start matching | Anchored to | Result if match | No match | r.lastIndex | |
---|---|---|---|---|---|---|
search() | –, /g | 0 | – | Index of match | -1 | unchanged |
/y, /gy | 0 | 0 | Index of match | -1 | unchanged | |
match() | – | 0 | – | Match object | null | unchanged |
/y | r.lastIndex |
r.lastIndex |
Match object | null | index after | |
match | ||||||
/g | After prev. | – | Array with matches | null | 0 | |
match (loop) | ||||||
/gy | After prev. | After prev. | Array with matches | null | 0 | |
match (loop) | match | |||||
split() | –, /g | After prev. | – | Array with strings | [str] |
unchanged |
match (loop) | between matches | |||||
/y, /gy | After prev. | After prev. | Arr. w/ empty strings | [str] |
unchanged | |
match (loop) | match | between matches | ||||
replace() | – | 0 | – | First match replaced | No repl. | unchanged |
/y | 0 | 0 | First match replaced | No repl. | unchanged | |
/g | After prev. | – | All matches replaced | No repl. | unchanged | |
match (loop) | ||||||
/gy | After prev. | After prev. | All matches replaced | No repl. | unchanged | |
match (loop) | match |
RegExp.prototype.exec(str)
If /g
is not set, matching always starts at the beginning, but skips ahead until a match is found. REGEX.lastIndex
is not changed.
If /g
is set, matching starts at REGEX.lastIndex
and skips ahead until a match is found. REGEX.lastIndex
is set to the position after the match. That means that you receive all matches if you loop until exec()
returns null
.
If only /y
is set, matching starts at REGEX.lastIndex
and is anchored to that position (no skipping ahead until a match is found). REGEX.lastIndex
is updated similarly to when /g
is set.
Setting both /y
and /g
is the same as only setting /y
.
RegExp.prototype.test(str)
test()
works the same as exec()
, but it returns true
or false
(instead of a match object or null
) when matching succeeds or fails:
String.prototype.search(regex)
search()
ignores the flag /g
and lastIndex
(which is not changed, either). Starting at the beginning of the string, it looks for the first match and returns its index (or -1
if there was no match):
If you set the flag /y
, lastIndex
is still ignored, but the regular expression is now anchored to index 0.
String.prototype.match(regex)
match()
has two modes:
/g
is not set, it works like exec()
./g
is set, it returns an Array with the string parts that matched, or null
.If the flag /g
is not set, match()
captures groups like exec()
:
If only the flag /g
is set then match()
returns all matching substrings in an Array (or null
). Matching always starts at position 0.
If you additionally set the flag /y
, then matching is still performed repeatedly, while anchoring the regular expression to the index after the previous match (or 0).
String.prototype.split(separator, limit)
The complete details of split()
are explained in Speaking JavaScript.
For ES6, it is interesting to see how things change if you use the flag /y
.
With /y
, the string must start with a separator:
Subsequent separators are only recognized if they immediately follow the first separator:
That means that the string before the first separator and the strings between separators are always empty.
As usual, you can use groups to put parts of the separators into the result array:
String.prototype.replace(search, replacement)
Without the flag /g
, replace()
only replaces the first match:
If only /y
is set, you also get at most one match, but that match is always anchored to the beginning of the string. lastIndex
is ignored and unchanged.
With /g
set, replace()
replaces all matches:
With /gy
set, replace()
replaces all matches, but each match is anchored to the end of the previous match:
The parameter replacement
can also be a function, consult “Speaking JavaScript” for details.
The main use case for sticky matching is tokenizing, turning a text into a sequence of tokens. One important trait about tokenizing is that tokens are fragments of the text and that there must be no gaps between them. Therefore, sticky matching is perfect here.
In a legal sequence of tokens, sticky matching and non-sticky matching produce the same output:
If, however, there is non-token text in the string then sticky matching stops tokenizing, while non-sticky matching skips the non-token text:
The behavior of sticky matching during tokenizing helps with error handling.
If you wanted to manually implement sticky matching, you’d do it as follows: The function execSticky()
works like RegExp.prototype.exec()
in sticky mode.
/u
(unicode) The flag /u
switches on a special Unicode mode for a regular expression. That mode has two features:
\u{1F42A}
for specifying characters via code points. Normal Unicode escapes such as \u03B1
only have a range of four hexadecimal digits (which equals the basic multilingual plane).A section in the chapter on Unicode has more information on escape sequences. I’ll explain the consequences of feature 2 next. Instead of Unicode code point escapes (e.g., \u{1F680}
), I’m using two UTF-16 code units (e.g., \uD83D\uDE80
). That makes it clear that surrogate pairs are grouped in Unicode mode and works in both Unicode mode and non-Unicode mode.
In non-Unicode mode, a lone surrogate in a regular expression is even found inside (surrogate pairs encoding) code points:
In Unicode mode, surrogate pairs become atomic units and lone surrogates are not found “inside” them:
Actual lone surrogate are still found:
In Unicode mode, you can put code points into character classes and they won’t be interpreted as two characters, anymore.
.
) matches code points, not code units In Unicode mode, the dot operator matches code points (one or two code units). In non-Unicode mode, it matches single code units. For example:
In Unicode mode, quantifiers apply to code points (one or two code units). In non-Unicode mode, they apply to single code units. For example:
flags
In ECMAScript 6, regular expressions have the following data properties:
source
flags
global
, ignoreCase
, multiline
, sticky
, unicode
lastIndex
As an aside, lastIndex
is the only instance property now, all other data properties are implemented via internal instance properties and getters such as get RegExp.prototype.global
.
The property source
(which already existed in ES5) contains the regular expression pattern as a string:
The property flags
is new, it contains the flags as a string, with one character per flag:
You can’t change the flags of an existing regular expression (ignoreCase
etc. have always been immutable), but flags
allows you to make a copy where the flags are changed:
The next section explains another way to make modified copies of regular expressions.
RegExp()
can be used as a copy constructor In ES6 there are two variants of the constructor RegExp()
(the second one is new):
new RegExp(pattern : string, flags = '')
pattern
. If flags
is missing, the empty string ''
is used.new RegExp(regex : RegExp, flags = regex.flags)
regex
is cloned. If flags
is provided then it determines the flags of the copy.The following interaction demonstrates the latter variant:
Therefore, the RegExp
constructor gives us another way to change flags:
exec()
The following function execAll()
is an iterable version of exec()
that fixes several issues with using exec()
to retrieve all matches of a regular expression:
exec()
until it returns null
).exec()
mutates the regular expression, which means that side effects can become a problem./g
must be set. Otherwise, only the first match is returned.Using execAll()
:
The following string methods now delegate some of their work to regular expression methods:
String.prototype.match
calls RegExp.prototype[Symbol.match]
.String.prototype.replace
calls RegExp.prototype[Symbol.replace]
.String.prototype.search
calls RegExp.prototype[Symbol.search]
.String.prototype.split
calls RegExp.prototype[Symbol.split]
.For more information, consult Sect. “String methods that delegate regular expression work to their parameters” in the chapter on strings.