このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

RegExp.prototype.compile()

Deprecated

Avoid using this feature in new projects. The compile() method duplicates behavior of the RegExp() constructor and it exists only for backwards compatibility. This feature may be a candidate for removal from web standards or browsers.

メモ: compile() メソッドは互換性のためにのみ定義されています。compile() を使用すると、それまで不変であった正規表現のソースとフラグが変更可能なものとなり、ユーザーの期待を裏切る可能性があります。代わりに RegExp() コンストラクターを使用して新しい正規表現オブジェクトを構築してください。

compile()RegExp インスタンスのメソッドで、 RegExp オブジェクトが既に作成された後、新しいソースとフラグで正規表現を再コンパイルするために使用します。

構文

js
compile(pattern, flags)

引数

pattern

正規表現のテキスト

flags

フラグ値の組み合わせです。

返値

なし (undefined)。

compile() の使用

次の例では、新しいパターンとフラグで正規表現を再コンパイルする方法を示します。

js
const regexObj = /foo/gi;
regexObj.compile("new foo", "g");

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-regexp.prototype.compile

ブラウザーの互換性

関連情報