HTTP/2 200
x-guploader-uploadid: AAwnv3IKYn_5-4HvCXNbxwjneScqechOy5BcZgBcV2kHCJu5LYDdg04lnlYDlLWmaEmh4oYp9Wujyuw
x-goog-generation: 1760058724778914
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 203535
x-goog-meta-goog-reserved-file-mtime: 1760057037
x-goog-hash: crc32c=7wuFnA==, md5=hbWXvd8lzRKVzAVuQHGU7Q==
x-goog-storage-class: STANDARD
accept-ranges: none
expires: Fri, 10 Oct 2025 12:19:56 GMT
cache-control: public, max-age=3600
last-modified: Fri, 10 Oct 2025 01:12:04 GMT
etag: W/"85b597bddf25cd1295cc056e407194ed"
content-type: text/html
age: 0
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
alt-svc: clear
referrer-policy: strict-origin-when-cross-origin
x-content-type-options: nosniff
strict-transport-security: max-age=63072000
content-security-policy: default-src 'self'; script-src 'report-sample' 'self' 'wasm-unsafe-eval' https://www.google-analytics.com/analytics.js https://www.googletagmanager.com/gtag/js assets.codepen.io production-assets.codepen.io https://js.stripe.com 'sha256-XNBp89FG76amD8BqrJzyflxOF9PaWPqPqvJfKZPCv7M=' 'sha256-YCNoU9DNiinACbd8n6UPyB/8vj0kXvhkOni9/06SuYw=' 'sha256-PZjP7OR6mBEtnvXIZfCZ5PuOlxoDF1LDZL8aj8c42rw='; script-src-elem 'report-sample' 'self' 'wasm-unsafe-eval' https://www.google-analytics.com/analytics.js https://www.googletagmanager.com/gtag/js assets.codepen.io production-assets.codepen.io https://js.stripe.com 'sha256-XNBp89FG76amD8BqrJzyflxOF9PaWPqPqvJfKZPCv7M=' 'sha256-YCNoU9DNiinACbd8n6UPyB/8vj0kXvhkOni9/06SuYw=' 'sha256-PZjP7OR6mBEtnvXIZfCZ5PuOlxoDF1LDZL8aj8c42rw='; style-src 'report-sample' 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self'; connect-src 'self' developer.allizom.org bcd.developer.allizom.org bcd.developer.mozilla.org updates.developer.allizom.org updates.developer.mozilla.org https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com https://incoming.telemetry.mozilla.org https://observatory-api.mdn.allizom.net https://observatory-api.mdn.mozilla.net https://api.github.com/search/issues stats.g.doubleclick.net https://api.stripe.com; font-src 'self'; frame-src 'self' interactive-examples.mdn.mozilla.net interactive-examples.mdn.allizom.net mdn.github.io live-samples.mdn.mozilla.net live-samples.mdn.allizom.net *.mdnplay.dev *.mdnyalp.dev *.play.test.mdn.allizom.net https://v2.scrimba.com https://scrimba.com jsfiddle.net www.youtube-nocookie.com codepen.io survey.alchemer.com https://js.stripe.com; img-src 'self' data: *.githubusercontent.com *.googleusercontent.com *.gravatar.com mozillausercontent.com firefoxusercontent.com profile.stage.mozaws.net profile.accounts.firefox.com developer.mozilla.org mdn.dev interactive-examples.mdn.mozilla.net interactive-examples.mdn.allizom.net wikipedia.org upload.wikimedia.org https://mdn.github.io/shared-assets/ https://mdn.dev/ https://*.google-analytics.com https://*.googletagmanager.com www.gstatic.com; manifest-src 'self'; media-src 'self' archive.org videos.cdn.mozilla.net https://mdn.github.io/shared-assets/; child-src 'self'; worker-src 'self';
x-frame-options: DENY
origin-trial: AxVILwizhbMjxFeHOn1P3R8niO1RJY/smaK4B4d1rLzc1gTaxtXMSaTi+FoigYgCw40uFRDwFcEAeqDR+vVLOW4AAABfeyJvcmlnaW4iOiJodHRwczovL2RldmVsb3Blci5tb3ppbGxhLm9yZyIsImZlYXR1cmUiOiJQcml2YXRlQXR0cmlidXRpb25WMiIsImV4cGlyeSI6MTc0MjA3OTYwMH0=
x-cloud-trace-context: ac3e16f7b14941d7d5ee9d17f5890aaf
date: Fri, 10 Oct 2025 11:19:56 GMT
server: Google Frontend
via: 1.1 google
vary: Accept-Encoding
content-encoding: gzip
x-cache: miss
数量子 - JavaScript | MDN
const ghostSpeak = "booh boooooooh";
const regexpSpooky = /bo{3,}h/;
console.log(ghostSpeak.match(regexpSpooky));
// 予想される結果: Array ["boooooooh"]
const modifiedQuote = "[He] ha[s] to go read this novel [Alice in Wonderland].";
const regexpModifications = /\[.*?\]/g;
console.log(modifiedQuote.match(regexpModifications));
// 予想される結果: Array ["[He]", "[s]", "[Alice in Wonderland]"]
const regexpTooGreedy = /\[.*\]/g;
console.log(modifiedQuote.match(regexpTooGreedy));
// 予想される結果: Array ["[He] ha[s] to go read this novel [Alice in Wonderland]"]
文字
意味
x *
直前のアイテム "x" の 0 回以上の繰り返しに一致します。例えば
/bo*/
は "A ghost booooed" の "boooo" や "A bird warbled"
の "b" に一致しますが、 "A goat grunted" には一致しません。
x +
直前のアイテム "x" の 1 回以上の繰り返しに一致します。{1,}
と同等です。例えば /a+/
は "candy" の "a" や
"caaaaaaandy" のすべての "a" に一致します。
x ?
直前のアイテム "x" の 0 回か 1 回の出現に一致します。例えば
/e?le?/
は "angel" の "el" や "angle" の "le"
に一致します。
*
、+
、?
、{}
といった数量子の直後に使用した場合、既定とは逆に、その数量子を非貪欲(出現回数が最小のものに一致)とします。既定は貪欲(出現回数が最大のものに一致)です。
x {n }
"n" には非負の整数が入ります。直前のアイテム "x" がちょうど "n" 回出現するものに一致します。例えば /a{2}/
は "candy" の "a" には一致しませんが、"caandy" のすべての "a"、"caaandy" の最初の 2 つの "a" に一致します。
x {n ,}
"n" には非負の整数が入ります。直前のアイテム "x" の少なくとも "n" 回の出現に一致します。例えば、/a{2,}/
は "candy" の "a" には一致しませんが、"caandy" や "caaaaaaandy" の "a" のすべてに一致します。
x {n ,m }
ここで、"n" と "m" は非負の整数で、m >= n
であリ、直前の項目 "x" に最小で "n" 回、最大で "m" 回一致します。
例えば /a{1,3}/
は "cndy" では一致せず、"candy" の 'a'、"caandy" の 最初の 2 個の "a"、"caaaaaaandy" の最初の 3 個の "a" に一致します。
"caaaaaaandy" では元の文字列に "a" が 4 個以上ありますが、一致するのは "aaa" であることに注意してください。
x *?
x +?
x ??
x {n}?
x {n,}?
x {n,m}?
既定では *
や +
といった数量子は貪欲です。つまり、できる限り多くの文字列と一致しようとします。数量子の後に ?
の文字を指定すると、数量子が「非貪欲」になります。つまり、一致が見つかるとすぐに停止します。例えば、"some <foo> <bar> new </bar> </foo> thing" といった文字列が与えられた場合は、
/<.*>/
は "<foo> <bar> new </bar> </foo>" に一致します。
/<.*?>/
は "<foo>" に一致します。
この例では、1 つ以上の英数文字を \w+
で、次に 1 つ以上の文字 "a" を a+
で、最後に単語の境界を \b
で照合します。
const wordEndingWithAs = /\w+a+\b/;
const delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
この例では、 1 文字だけの単語、 2 文字以上 6 文字以下の単語、 13 文字以上の単語を検索します。
const singleLetterWord = /\b\w\b/g;
const notSoLongWord = /\b\w{2,6}\b/g;
const longWord = /\b\w{13,}\b/g;
const sentence = "Why do I have to learn multiplication table?";
console.table(sentence.match(singleLetterWord)); // ["I"]
console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "have", "to", "learn", "table" ]
console.table(sentence.match(longWord)); // ["multiplication"]
この例では、 "our" または "or" で終わる単語を検索します。
const britishText = "He asked his neighbour a favour.";
const americanText = "He asked his neighbor a favor.";
const regexpEnding = /\w+ou?r/g;
// \w+ 1 つ以上の文字
// o "o" が続く
// u? 省略可能で "u" が続く
// r "r" が続く
console.table(britishText.match(regexpEnding));
// ["neighbour", "favour"]
console.table(americanText.match(regexpEnding));
// ["neighbor", "favor"]
この例では、 1 つ以上の単語文字または空白文字を [\w ]+
と [\w ]+?
で検索します。 1 つ目は貪欲で、 2 つ目は貪欲ではありません。 2 つ目は最小要件を満たすとすぐに停止することに注意してください。
const text = "I must be getting somewhere near the center of the earth.";
const greedyRegexp = /[\w ]+/;
console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the center of the earth"
// テキストのすべてに一致(ピリオドを除く)
const nonGreedyRegexp = /[\w ]+?/; // 疑問符に注目
console.log(text.match(nonGreedyRegexp));
// "I"
// 一致する箇所は取りうる最も短い 1 文字