RegExp.prototype.unicode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

unicodeRegExp インスタンスのアクセサープロパティで、この正規表現に u フラグが使用されているかどうかを返します。

試してみましょう

const regex1 = new RegExp("\u{61}");
const regex2 = new RegExp("\u{61}", "u");

console.log(regex1.unicode);
// Expected output: false

console.log(regex2.unicode);
// Expected output: true

console.log(regex1.source);
// Expected output: "a"

console.log(regex2.source);
// Expected output: "a"

解説

RegExp.prototype.unicode の値は true ならば u フラグが使用されていることを示し、そうでなければ false となります。u フラグは Unicode に関連する様々な機能を有効にします。 "u" フラグを使用すると、次のようになります。

  • Unicode コードポイントエスケープ ( \u{xxxx}, \p{UnicodePropertyValue}) は、ID エスケープの代わりにそのように解釈されます。例えば、 /\u{61}/u"a" に一致しますが、/\u{61}/u フラグなし)は "u".repeat(61) に一致します。 \uu 1 文字と同等になるからです。
  • サロゲートペアは 2 つの別々の文字ではなく、全体の文字として解釈されます。例えば /[😄]/u"😄" にのみ一致し、"\ud83d" には一致しません。
  • lastIndex が自動的に進む場合(exec() を呼び出した場合など)、Unicode 正規表現は UTF-16 コード単位ではなく Unicode コードポイント単位で進みます。

構文の間違いを防ぐために、構文解析の動作を変更したものが他にもあります(正規表現構文の厳格モードに似ています)。これらの構文はすべて非推奨であり、ウェブの互換性のためだけに残されているもの ですので、頼らないでください。

unicode の設定アクセサーは undefined です。このプロパティを直接変更することはできません。

Unicode 対応モード

Unicode 対応モードと言う言葉を使うときは、u または v のどちらかのフラグがついた正規表現を指し、この場合は正規表現が Unicode に関連した機能(Unicode 文字クラスエスケープなど)が利用できるようになり、もっと厳格化した構文ルールになります。uv は同じ正規表現を互換性のない方法で解釈するため、両方のフラグを使用すると SyntaxError になります。

同様に、正規表現が u フラグも v フラグも持たない場合、Unicode 非対応モードとなります。この場合、正規表現は UTF-16 コード単位の並びとして解釈され、たくさんの古い構文が構文エラーにならなくなります。

unicode プロパティの使用

js
const regex = /\u{61}/u;

console.log(regex.unicode); // true

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-get-regexp.prototype.unicode

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
unicode

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

関連情報