Regular Expressions
Note
Unless noted, all of the following is written with recent versions of JavaScript in mind. Different environments may support different regex features!
General
- default to using the
v
(widely supported from September 2023 onwards) oru
(for older browsers) flags to avoid bugs related to Unicode handling
Match superscript and subscript numbers
\p{No}
Groups
(?:...) | non-capturing group |
(?<name>) | named capturing group (the angle brackets are part of the syntax!) |
Lookarounds
- look for characters around the match, without adding those characters to the match
- break them them down into parts:
?
marks a "special" group (like the ones above)<
for behind (optional)=
for positive or!
for negative
(?=...) | positive lookahead |
(?!...) | negative lookahead |
(?<=...) | positive lookbehind |
(?<!...) | negative lookbehind |
Lazy quantifiers
Add ?
after any quantifier to make it lazy (match as few characters as possible).
For example, given the text: buffalo buffalo buffalo
/buffalo.+buffalo/
matches: buffalo buffalo buffalo
/buffalo.+?buffalo/
matches: buffalo buffalo buffalo
Substitutions
$& | contents of match |
$` | contents before match |
$' | contents after match |
$1 | contents of capture group 1 |
Change case (VSCode)
Warning
These work in VSCode, but not in JavaScript!
\U | start uppercasing |
\L | start lowercasing |
\E | stop changing case |
ex. Title case Markdown headers:
Find:
(#\s*.)(.+)$
Replace:
\U$1\L$2
JavaScript
regexp.test(str)
- returns true/falsestr.search(regexp)
- returns match index (-1 if not found)regexp.exec(str)
- returns [match, ...captureGroups]- array has extra properties:
index
,input
,groups
(named capturing groups),indices
(start/end indices of each capture group, only ifd
flag is used)
- array has extra properties:
str.match(regexp)
- if
g
flag is not used, same asregexp.exec
- if
g
flag is used, returns array of all matches
- if
str.matchAll(regexp)
- returns iterator ofRegExp.exec
results- can turn it into an array using
[...str.matchAll(regexp)]
- can turn it into an array using