RegExp【正規表現:Regular Expression】オブジェクト
メモ コンストラクタ プロパティ 一覧 メソッド 一覧 関連オブジェクト・メソッド 検索フラグ 正規表現パターン 正規表現パターン例 例
メモ
概要
基本処理
〔 例 〕- RegExp【正規表現】使用
- String【文字列】使用
- String【文字列】の match【正規表現検索 (詳細)】 ・replace【置換 (正規表現可・関数指定可)】メソッド等で検索・置換・分割
注意事項
- 処理時間のかかる正規表現に注意
- ネスト:(例)
/^([0-9]+)*$/
に対して "9~9#" - 重複:(例)
/(x|xx)+/
- 文字長の制限なし
- ネスト:(例)
外部リンク
- ECMA-262 (英語)
RegExp (Regular Expression) Objects ES2023 (14) ES2022 (13) ES2021 (12) Patterns ES2023 (14) ES2022 (13) ES2021 (12) Pattern Semantics ES2023 (14) ES2022 (13) ES2021 (12) Grammar Summary - Regular Expressions ES2023 (14) ES2022 (13) ES2021 (12) Properties of the RegExp Constructor ES2023 (14) ES2022 (13) ES2021 (12) Properties of the RegExp Prototype Object ES2023 (14) ES2022 (13) ES2021 (12) Properties of RegExp Instances ES2023 (14) ES2022 (13) ES2021 (12)
正規表現リテラル・コンストラクタ
構文 | 説明 | 引数 |
---|---|---|
/pattern/ [flags] | 正規表現リテラル | pattern:正規表現パターン flags:検索フラグ |
[new] RegExp ( "pattern" [, "flags" ] ) | コンストラクタ |
プロパティ 一覧〔 例 〕
プロパティ | 説明 | |
---|---|---|
RegExp. | $1 ~ $9 | キャプチャ文字列 (正規表現パターン参照) |
RegExp | [ @@species ] | コンストラクタ定義 (実装:[Symbol.species]) |
RegExp.prototype. | constructor | コンストラクタ定義 |
RegExp.prototype. | dotAll | 's'【改行文字対応】フラグ 使用有無 (true / false) |
RegExp.prototype. | flags | 検索フラグ ("gimsuy" の内で使用している検索フラグの文字列) |
RegExp.prototype. | global | 'g'【グローバル検索】フラグ 使用有無 (true / false) |
RegExp.prototype. | hasIndices | 'd'【キャプチャ部分の範囲インデックス取得】フラグ 使用有無 (true / false) |
RegExp.prototype. | ignoreCase | 'i'【大文字小文字区別なし】フラグ 使用有無 (true / false) |
RegExp. | input ($_) | 検索文字列 |
RegExp. | lastIndex | 次回検索の開始位置 (初期値:0 / 書き込み可能) 'g'【グローバル検索】 または 'y'【検索開始位置固定】フラグ 指定で利用 |
RegExp. | lastMatch ($&) | 最後に一致した文字列 (~.$& は使用不可 → ~["$&"] を使用) |
RegExp. | lastParen ($+) | 最後の括弧内の部分文字列の一致 (~.$+ は使用不可 → ~["$+"] を使用) |
RegExp. | leftContext ($`) | 最後に一致した左側の部分文字列 (~.$` は使用不可 → ~["$`"] を使用) |
RegExp.prototype. | multiline | 'm'【複数行検索】フラグ使用有無 (true / false) |
RegExp. | prototype | プロトタイプ |
RegExp. | rightContext ($') | 最後に一致した右側の部分文字列 (~.$' は使用不可 → ~["$'"] を使用) |
RegExp.prototype. | source | 正規表現パターン |
RegExp.prototype. | sticky | 'y'【検索開始位置固定】フラグ使用有無 (true / false) |
RegExp.prototype. | unicode | 'u'【Unicode】フラグ使用有無 (true / false) |
メソッド 一覧
メソッド | 説明 | |
---|---|---|
RegExp.prototype | [ @@match ] ( string ) | 検索 (詳細) (実装:[Symbol.match]) String.match()【正規表現検索 (詳細)】 から呼び出し 正規表現判定 (Boolean 型):falseの場合、下記メソッドの例外を回避 String.startsWith【開始文字列判定】 String.endsWith【終了文字列判定】 String.includes【部分文字列判定】 |
RegExp.prototype | [ @@matchAll ] ( string ) | 検索 (詳細イテレータ) (実装:[Symbol.matchAll]) String.matchAll()【正規表現検索 (詳細イテレータ)】 から呼び出し |
RegExp.prototype | [ @@replace ] ( string, replaceValue ) | 置換 (関数指定可) (実装:[Symbol.replace]) String.replace()【置換 (正規表現可・関数指定可)】 から呼び出し |
RegExp.prototype | [ @@search ] ( string ) | 検索 (簡易) (実装:[Symbol.search]) String.search()【正規表現検索 (詳細)】 から呼び出し |
RegExp.prototype | [ @@split ] ( string, limit ) | 分割 (実装:[Symbol.split]) String.split()【分割 (正規表現可)】 から呼び出し |
RegExp.prototype. | compile(pattern, flags ) | コンパイル |
RegExp.prototype. | exec( string ) | 検索 |
RegExp.prototype. | test( string ) | 検索評価 |
RegExp.prototype. | toString() | 文字列変換 |
関連オブジェクト・メソッド
正規表現の指定が可能なその他のオブジェクト・メソッド
オブジェクト | メソッド | 説明 |
---|---|---|
String【文字列】オブジェクト | match ( regexp ) | 正規表現検索 (詳細) |
matchAll ( regexp ) | 正規表現検索 (詳細イテレータ) | |
replace ( searchValue, replaceValue ) | 置換 (正規表現可・関数指定可) | |
replaceAll ( searchValue, replaceValue ) | 全置換 (正規表現可・関数指定可) | |
search ( regexp ) | 正規表現検索 (簡易) | |
split ( [separator [, limit]] ) | 分割 (正規表現可) |
検索フラグ
検索フラグ | プロパティ | 説明 |
---|---|---|
d | hasIndices | キャプチャ部分の範囲インデックス取得 (取得結果は、exec【検索】参照) |
g | global | グローバル検索 (lastIndex【次回検索の開始位置】プロパティを使用し、次回の処理に利用) |
i | ignoreCase | 大文字小文字の区別なし |
m | multiline | 複数行の検索 (^:行頭 / $:行末) |
s | dotAll | 改行文字対応 (.【改行以外の 1 文字】に改行を含む) |
u | unicode | Unicode対応 (コードポイント値を使用し、サロゲートペア対応とする) |
y | sticky | 検索開始位置固定 (lastIndex【次回検索の開始位置】プロパティを開始位置に固定して処理) |
正規表現パターン
正規表現パターン | 説明 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
^ | 先頭 'm'【複数行検索】フラグが指定された場合、行頭 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$ | 末尾 'm'【複数行検索】フラグが指定された場合、行末 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
. | 改行以外の 1 文字 ('s'【改行文字対応】フラグ が指定された場合、改行も含む) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン* | パターン の 0 回以上の繰り返し (最長一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン*? | パターン の 0 回以上の繰り返し (最短一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン+ | パターン の 1 回以上の繰り返し (最長一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン+? | パターン の 1 回以上の繰り返し (最短一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン? | パターン の 0 回か 1 回 (最長一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン?? | パターン の 0 回か 1 回 (最短一致) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン{n} | パターン の n 回の繰り返し (最長一致 / n:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン{n}? | パターン の n 回の繰り返し (最短一致 / n:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン{n,} | パターン の n 回以上の繰り返し (最長一致 / n:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン{n,}? | パターン の n 回以上の繰り返し (最短一致 / n:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン{n,m} | パターン の n 回 ~ m 回の繰り返し (最長一致 / n・m:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン {n,m}? | パターン の n 回 ~ m 回の繰り返し (最短一致 / n・m:正数) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターンA|パターンB | パターン を区切り、どちらかの パターン と一致 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[文字の集合] | 文字の集合 内の文字 (- で範囲指定) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[^文字の集合] | 文字の集合 内の文字以外 (- で範囲指定) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(パターン) | 後方参照ありのキャプチャ (後方参照:$1~$9) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(?:パターン) | 後方参照なしのキャプチャ (後方参照不可) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(?<キャプチャ名> パターン) | 名前付きキャプチャ (例:exec【検索】の 例 ) 後方参照:\k<キャプチャ名> または \1 ~ \9 メソッド等の参照:$<キャプチャ名> または $1 ~ $9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン(?=パターン (後)) | 肯定先読み:パターン に パターン (後) が続く | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
パターン(?!パターン (後)) | 否定先読み:パターン に パターン (後) が続かない | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(?<=パターン (前)) パターン | 肯定後読み:パターン (前) に パターン が続く | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(?<!パターン (前)) パターン | 否定後読み:パターン (前) でない後に パターン が続く | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\~ | エスケープシーケンス | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\1 ~ \9 | キャプチャの後方参照 (名前付きキャプチャも含む) (メソッド等からは $1 ~ $9 で参照) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\k<キャプチャ名> | 名前付きキャプチャの後方参照 (メソッド等からは $<キャプチャ名> で参照) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\b | 単語の区切り | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\B | 単語の区切り以外 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\d | 数字 ( '[0-9]' に相当 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\D | 数字以外 ( '[^\d]' に相当 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\f | 改ページ (\u000C:form feed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\n | 改行 (\u000A:line feed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\p{プロパティ名} \p{プロパティ値} \p{プロパティ名=プロパティ値} | プロパティ名 を持つ Unicode文字 プロパティ値 を持つ Unicode文字 プロパティ名 の値が プロパティ値 である Unicode文字 'u'【Unicode対応】フラグ指定 プロパティ名・プロパティ値は外部リンクの参照 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\P{プロパティ名} \P{プロパティ値} \P{プロパティ名=プロパティ値} | プロパティ名 を持たない Unicode文字 プロパティ値 を持たない Unicode文字 プロパティ名 の値が プロパティ値 でない Unicode文字 'u'【Unicode対応】フラグ指定 プロパティ名・プロパティ値は外部リンクの参照 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\r | 復帰 (\u000D:carriage return) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\s | 空白文字 (下記が対象)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\S | 空白文字以外 ( '[^\s]' に相当 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\t | タブ (\u0009:horizontal tab) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\uHHHH | 16進数定義 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\v | 垂直タブ (\u000B:vertical tab) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\w | 文字 (半角英数字 及び アンダースコア(_)) ( '[A-Za-z0-9_]' に相当 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\W | 文字以外 ( '[^\w]' に相当 ) |
パターン:検索位置移動
パターン:検索位置移動なし
正規表現パターン例
検索対象 | 正規表現パターン |
---|---|
数字 | [0-9] |
半角英字 | [A-Za-z] |
半角英大文字 | [A-Z] |
半角英小文字 | [a-z] |
ひらがな | [ぁ-ん] |
全角カタカナ | [ァ-ヴ] |
半角カタカナ | [ヲ-゚] |
改行を含む任意の文字 | [\s\S] . ('s'【改行文字対応】フラグ指定) |
Eメールアドレス※ (HTML 5.1・WHATWG) | /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ |
- ※Eメールアドレス
- 正規表現で表すことはほぼ不可能 (コメントが可能等)
- <input>【汎用入力コントロール】のEメールアドレスで定義 (RFC 5322 を意図的に違反)
例
基本処理
// 0123456789+123456789+123456789+123456789+1234
const str = "The quick brown fox jumps over the lazy dog.";
// RegExp.exec (1/2)
const regex1 = /brown (\w+)/;
let result1 = regex1.exec(str);
console.log(result1);
// 出力例:
// (2) ['brown fox', 'fox', index: 10, input: 'The quick brown fox jumps over the lazy dog.', groups: undefined]
// 0: "brown fox"
// 1: "fox"
// groups: undefined
// index: 10
// input: "The quick brown fox jumps over the lazy dog."
// length: 2
// [[Prototype]]: Array(0)
// RegExp.exec (2/2)
const regex2 = new RegExp("lazy (\\w+)");
let result2 = regex2.exec(str);
console.log(result2);
// 出力例:
// (2) ['lazy dog', 'dog', index: 35, input: 'The quick brown fox jumps over the lazy dog.', groups: undefined]
// 0: "lazy dog"
// 1: "dog"
// groups: undefined
// index: 35
// input: "The quick brown fox jumps over the lazy dog."
// length: 2
// [[Prototype]]: Array(0)
// String.match
let result3 = str.match(/brown (\w+)/);
console.log(result3);
// 出力例:
// (2) ['brown fox', 'fox', index: 10, input: 'The quick brown fox jumps over the lazy dog.', groups: undefined]
// 0: "brown fox"
// 1: "fox"
// groups: undefined
// index: 10
// input: "The quick brown fox jumps over the lazy dog."
// length: 2
// [[Prototype]]: Array(0)
// String.replace
let result4 = str.replace(/(lazy) \w+/, "$1 'tanuki'");
console.log(result4);
// 出力例:
// The quick brown fox jumps over the lazy 'tanuki'.
プロパティ
console.log(RegExp[Symbol.species]); // 出力:function RegExp() { [native code] }
console.log(RegExp.prototype); // 出力:Object {...}
var regex = /([0-9]+)-([0-9]+)-([0-9]+)/;
console.log(regex.constructor); // 出力:function RegExp() { [native code] }
console.log(regex.source); // 出力:([0-9]+)-([0-9]+)-([0-9]+)
console.log(regex.test("123-abc-456")); // 出力:false
console.log(regex.test("123-456-789")); // 出力:true
console.log(RegExp.$1, RegExp.$2, RegExp.$3); // 出力:123 456 789
var regex = /^([a-z]+)-([a-z]+)-([a-z]+)/gim;
console.log(regex.flags, regex.global, regex.ignoreCase, regex.multiline, regex.sticky, regex.unicode);
// 出力:gim true true true false false
var str = "AAA-bbb-CCC-ddd-EEE\nfff-GGG-hhh-III-jjj";
var i = 1;
while (true) {
var result = regex.exec(str);
if (result == null) {
break;
}
console.log("[ " + i++ + " 回目 ]");
console.log(result);
console.log(regex.lastIndex);
console.log(RegExp.$1, RegExp.$2, RegExp.$3);
console.log("RegExp.input = [" + RegExp.input + "]");
console.log("RegExp.$_ = [" + RegExp.$_ + "]");
console.log("RegExp.lastMatch = [" + RegExp.lastMatch + "]");
console.log("RegExp[\"$&\"] = [" + RegExp["$&"] + "]");
console.log("RegExp.lastParen = [" + RegExp.lastParen + "]");
console.log("RegExp[\"$+\"] = [" + RegExp["$+"] + "]");
console.log("RegExp.leftContext = [" + RegExp.leftContext + "]");
console.log("RegExp[\"$`\"] = [" + RegExp["$`"] + "]");
console.log("RegExp.rightContext = [" + RegExp.rightContext + "]");
console.log("RegExp[\"$'\"] = [" + RegExp["$'"] + "]");
}
// 出力:[ 1 回目 ]
// 出力:["AAA-bbb-CCC", "AAA", "bbb", "CCC", index: 0, input: "AAA-bbb-CCC-ddd-EEE↵fff-GGG-hhh-III-jjj"]0: "AAA-bbb-CCC"1: "AAA"2: "bbb"3: "CCC"index: 0input: "AAA-bbb-CCC-ddd-EEE↵fff-GGG-hhh-III-jjj"length: 4__proto__: Array[0]
// 出力:11
// 出力:AAA bbb CCC
// 出力:RegExp.input = [AAA-bbb-CCC-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:RegExp.$_ = [AAA-bbb-CCC-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:RegExp.lastMatch = [AAA-bbb-CCC]
// 出力:RegExp["$&"] = [AAA-bbb-CCC]
// 出力:RegExp.lastParen = [CCC]
// 出力:RegExp["$+"] = [CCC]
// 出力:RegExp.leftContext = []
// 出力:RegExp["$`"] = []
// 出力:RegExp.rightContext = [-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:RegExp["$'"] = [-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:[ 2 回目 ]
// 出力:["fff-GGG-hhh", "fff", "GGG", "hhh", index: 20, input: "AAA-bbb-CCC-ddd-EEE↵fff-GGG-hhh-III-jjj"]
// 出力:31
// 出力:fff GGG hhh
// 出力:RegExp.input = [AAA-bbb-CCC-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:RegExp.$_ = [AAA-bbb-CCC-ddd-EEE
// 出力:fff-GGG-hhh-III-jjj]
// 出力:RegExp.lastMatch = [fff-GGG-hhh]
// 出力:RegExp["$&"] = [fff-GGG-hhh]
// 出力:RegExp.lastParen = [hhh]
// 出力:RegExp["$+"] = [hhh]
// 出力:RegExp.leftContext = [AAA-bbb-CCC-ddd-EEE
// 出力:]
// 出力:RegExp["$`"] = [AAA-bbb-CCC-ddd-EEE
// 出力:]
// 出力:RegExp.rightContext = [-III-jjj]
// 出力:RegExp["$'"] = [-III-jjj]
// 【'y'(検索開始位置固定) フラグ】
var str = "999aaaAAA";
var regex = /[a-z]+[A-Z]+/;
console.log(regex.test(str)); // 出力:true
var regexY = /[a-z]+[A-Z]+/y;
regexY.lastIndex = 0;
console.log(regexY.test(str)); // 出力:false
regexY.lastIndex = 3;
console.log(regexY.test(str)); // 出力:true
// 【'u'(Unicode) フラグ】
// 叱る (第1水準)
// 𠮟る (第3水準) \u{20B9F}
var str = "叱る・\u{20B9F}る"
console.log(str); // 出力:叱る・𠮟る
// 𠮟 が2文字と認識
var regex = /.る/g;
while (true) {
var result = regex.exec(str);
if (result == null) {
break;
}
console.log(result);
}
// 出力:["叱る", index: 0, input: "叱る・𠮟る"]
// 出力:["(不正文字)る", index: 4, input: "叱る・𠮟る"]
// 𠮟 が1文字と認識
var regexU = /.る/gu;
while (true) {
var result = regexU.exec(str);
if (result == null) {
break;
}
console.log(result);
}
// 出力:["叱る", index: 0, input: "叱る・𠮟る"]
// 出力:["𠮟る", index: 3, input: "叱る・𠮟る"]