Regex
Cheatsheet
Character classes
.any character except newline\w \d \sword, digit, whitespace[abc](Set) --- any of a, b, or c[a-g](Range) --- character between a & g
Negative Character classes
\W \D \Snot word, digit, whitespace[^abc](Set) --- not a, b, or c
Anchors
^someWord$start / end of the string\bword boundary\Bnot-word boundary
Escaped characters
\. \* \\ \[escaped special characters\t \n \rtab, linefeed, carriage return
Groups & Lookaround
(someWord)capture group\1backreference to group #1(?:someWord)non-capturing group(?=someWord)positive lookahead(?!someWord)negative lookahead(?<=someWord)positive lookbehind(?<!someWord)negative lookbehind
Quantifiers
直前の文字等の数を指定するための文法
a*0 or morea+1 or morea?0 or 1a{5}exactly fivea{2,}two or morea{2,4}between two & foura{,4}four or lessa*? a+? a{2,}?match as few as possible (lazy mode, non-greedy mode)
Alternation
ab|cdab or cd
global match
/,/ // マッチする最初のカンマ
/,/g // マッチする全てのカンマ
multi line
- マルチラインモードでは
.*とすると行をまたげない(.は改行以外の任意の一文字のため)。 行をまたぐには、[\s\S]*とする必要がある。 - なお、下記の例では
*を*?とすることで、lazy(non-greedy)モードで検索している。 この場合、マッチする最短の文字列が選ばれる。
/<br>[\s\S]*?<br>/m;
case-insensitive search
/[a-z]/; // 小文字のa-z
/[a-z]/i; // 小文字・大文字のa-z