正規表現チートシート

検索可能な構文ガイド付きの対話型正規表現リファレンス。ライブマッチングでパターンをテストできます。文字クラス、量指定子、グループ、フラグに対応しています。

Showing 39 of 39 entries

Character Classes

10 entries
.

Any character

Matches any single character except newline (unless s flag is set)

Pattern: c.tMatches: "cat", "cut", "c3t"
\d

Digit

Matches any digit character [0-9]

Pattern: \d+Matches: "42", "007"
\D

Non-digit

Matches any character that is not a digit

Pattern: \D+Matches: "abc", "foo!"
\w

Word character

Matches any alphanumeric character or underscore [A-Za-z0-9_]

Pattern: \w+Matches: "hello", "foo_42"
\W

Non-word character

Matches any character that is not a word character

Pattern: \W+Matches: " ", "!@#"
\s

Whitespace

Matches any whitespace character: space, tab, newline, carriage return, form feed

Pattern: \s+Matches: " ", "\t\n"
\S

Non-whitespace

Matches any character that is not whitespace

Pattern: \S+Matches: "hello", "word"
[abc]

Character set

Matches any single character listed inside the brackets

Pattern: [aeiou]Matches: "a", "e", "i", "o", "u"
[^abc]

Negated set

Matches any single character NOT listed inside the brackets

Pattern: [^aeiou]Matches: "b", "c", "d" (not vowels)
[a-z]

Character range

Matches any character in the specified Unicode range. Combine ranges: [a-zA-Z0-9]

Pattern: [a-z]+Matches: "hello", "world"

Anchors

4 entries
^

Start of string

Matches the position at the start of the string (or line with m flag)

Pattern: ^HelloMatches: "Hello world" (start only)
$

End of string

Matches the position at the end of the string (or line with m flag)

Pattern: world$Matches: "Hello world" (end only)
\b

Word boundary

Matches the position between a word character and a non-word character

Pattern: \bcat\bMatches: "cat" but not "catfish"
\B

Non-word boundary

Matches any position that is not a word boundary

Pattern: \Bcat\BMatches: "concatenate" (mid-word)

Quantifiers

8 entries
*

Zero or more (greedy)

Matches the preceding element zero or more times, as many as possible

Pattern: ab*cMatches: "ac", "abc", "abbc"
+

One or more (greedy)

Matches the preceding element one or more times, as many as possible

Pattern: ab+cMatches: "abc", "abbc" (not "ac")
?

Zero or one

Matches the preceding element zero or one time (makes it optional)

Pattern: colou?rMatches: "color", "colour"
{n}

Exactly n

Matches the preceding element exactly n times

Pattern: \d{4}Matches: "2024", "1234"
{n,}

n or more

Matches the preceding element n or more times

Pattern: \d{2,}Matches: "12", "999", "1000"
{n,m}

Between n and m

Matches the preceding element between n and m times (inclusive)

Pattern: \d{2,4}Matches: "12", "123", "1234"
*?

Zero or more (lazy)

Matches the preceding element zero or more times, as few as possible

Pattern: <.+?>Matches: "<b>" in "<b>bold</b>"
+?

One or more (lazy)

Matches the preceding element one or more times, as few as possible

Pattern: "[^"]+?"Matches: Shortest quoted string

Groups

6 entries
(abc)

Capturing group

Groups the expression and captures the matched text. Accessible as \1, \2, or match[1]

Pattern: (\d{4})Matches: Captures year in "2024-01-15"
(?:abc)

Non-capturing group

Groups the expression without creating a capture. Use to apply quantifiers without capturing

Pattern: (?:ab)+Matches: "ab", "abab", "ababab"
(?=abc)

Positive lookahead

Asserts that what follows the current position matches the pattern (zero-width)

Pattern: \d+(?= USD)Matches: "100" in "100 USD"
(?!abc)

Negative lookahead

Asserts that what follows does NOT match the pattern (zero-width)

Pattern: \d+(?! USD)Matches: "42" in "42 EUR"
(?<=abc)

Positive lookbehind

Asserts that what precedes the current position matches the pattern (zero-width)

Pattern: (?<=\$)\d+Matches: "100" in "$100"
(?<!abc)

Negative lookbehind

Asserts that what precedes does NOT match the pattern (zero-width)

Pattern: (?<!\$)\d+Matches: "42" but not "$42"

Flags

6 entries
g

Global

Find all matches rather than stopping after the first match

Pattern: /\d+/gMatches: All numbers in a string
i

Ignore case

Makes the pattern case-insensitive

Pattern: /hello/iMatches: "hello", "HELLO", "Hello"
m

Multiline

Makes ^ match the start of each line and $ match the end of each line

Pattern: /^\w+/mMatches: First word of each line
s

DotAll

Makes . match any character including newline characters

Pattern: /a.b/sMatches: "a\nb" across newlines
u

Unicode

Enables full Unicode matching; required for \p{...} property escapes and 4-byte Unicode code points

Pattern: /\p{L}+/uMatches: Any Unicode letter
y

Sticky

Matches only from the position indicated by lastIndex; does not advance past it

Pattern: /\d+/yMatches: Only at exact lastIndex position

Special

5 entries
|

Alternation

Matches either the expression before or after the pipe. Acts like OR

Pattern: cat|dogMatches: "cat" or "dog"
\1

Backreference

Matches the same text that was matched by capturing group 1. \2 for group 2, etc.

Pattern: (\w+)\s\1Matches: "the the", "ha ha"
\n

Newline

Matches a newline character (line feed, LF)

Pattern: \r?\nMatches: Unix or Windows line endings
\t

Tab

Matches a horizontal tab character

Pattern: \t+Matches: Tab-indented lines
\\

Literal backslash

Matches a literal backslash character. Backslash must be escaped as \\

Pattern: C:\\\w+Matches: "C:\Users" in file paths

Quick Test

— paste a pattern and see matches highlighted
//g

正規表現チートシートの使い方

  1. 1カテゴリー別に正規表現の構文を閲覧します。
  2. 2特定のパターンまたは概念を検索します。
  3. 3ライブマッチャーで正規表現をテストします。
  4. 4コード内で使用するパターンをコピーします。
Zenovayアナリティクス

創業者のためのアナリティクス。

  • リアルタイムの訪問者トラッキング
  • プライバシーファースト、クッキーバナーなし
  • 2分でセットアップ
Zenovayを見る

よくある質問

正規表現(regex)とは何ですか?
正規表現は検索パターンを定義する文字シーケンスです。正規表現は文字列マッチング、検証、検索置換、テキスト抽出に使用されます。ほぼすべてのプログラミング言語でサポートされています。
異なる正規表現のフレーバーは何ですか?
主なフレーバー:PCRE(PHP、Python re)、JavaScript(ECMAScript)、.NET、Java、POSIX。ルックビハインド、名前付きグループ、Unicode対応などの機能に違いがあります。JavaScriptの正規表現はES2018で名前付きグループとルックビハインドなどの機能を追加しました。
貪欲マッチングと遅延マッチングとは何ですか?
貪欲な量指定子(*、+、?)は可能な限り多くマッチします。遅延(非貪欲)量指定子(*?、+?、??)は可能な限り少なくマッチします。例:'<b>bold</b>'の場合、/<.*>/は'<b>bold</b>'(貪欲)にマッチしますが、/<.*?>/は'<b>'(遅延)にマッチします。
ルックアヘッドとルックビハインドとは何ですか?
ルックアヘッド(?=...)とルックビハインド(?<=...)は、文字を消費せずにマッチするゼロ幅アサーションです。正のルックアヘッド:\d(?=px)は'px'に続く数字にマッチします。負のルックビハインド:(?<!\$)\d+は'$'に先行されていない数字にマッチします。
一般的な正規表現の間違いは何ですか?
特殊文字をエスケープしない(.は任意の文字にマッチし、リテラルドットではない)。ネストされた量指定子での破局的なバックトラック。より具体的なパターンが機能する場合に.*を使用する。パターンを固定しない(^と$を忘れる)。適切なパーサーの代わりにHTMLやXML解析に正規表現を使用する。