入力境界アサーション: ^, $

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.

入力境界アサーションは、文字列の減殺の位置が入力境界であるかどうかを検査します。入力境界は文字列の先頭または末尾です。m フラグが設定された場合は、行の先頭または末尾です。

構文

regex
^
$

解説

^ は現在の位置が入力の先頭であるかどうかを検査します。$ は現在の位置が入力の末尾であるかどうかを検査します。どちらもアサーションであり、何も文字を消費しません。

より正確には、^ は左側の文字が文字列の範囲外であることを、$ は右側の文字が文字列の範囲外であることを表明します。m フラグが設定されている場合、^ は左の文字が改行文字である場合にも一致し、$ は右の文字が改行文字である場合にも一致します。

m フラグが設定されていない限り、^$ のアサーションはパターンの境界に配置したときのみ意味を持ちます。その左右に他の文字があるとアサーションが発生しなくなるからです。

y フラグはこれらのアサーションの意味を変えません。アンカー粘着フラグも参照してください。

末尾のスラッシュの除去

次の例は、URL 文字列から末尾のスラッシュを解除します。

js
function removeTrailingSlash(url) {
  return url.replace(/\/$/, "");
}

removeTrailingSlash("https://example.com/"); // "https://example.com"
removeTrailingSlash("https://example.com/docs/"); // "https://example.com/docs"

ファイル拡張子の照合

次の例は、常に文字列の最後に来るファイル拡張子を照合することで、ファイル型を調べます。

js
function isImage(filename) {
  return /\.(?:png|jpe?g|webp|avif|gif)$/i.test(filename);
}

isImage("image.png"); // true
isImage("image.jpg"); // true
isImage("image.pdf"); // false

入力全体の照合

正規表現が入力の部分文字列だけでなく、入力全体と一致することを確認したい場合があります。例えば、文字列が有効な識別子かどうかを判定する場合、パターンの両端に入力境界のアサーションを追加することができます。

js
function isValidIdentifier(str) {
  return /^[$_\p{ID_Start}][$_\p{ID_Continue}]*$/u.test(str);
}

isValidIdentifier("foo"); // true
isValidIdentifier("$1"); // true
isValidIdentifier("1foo"); // false
isValidIdentifier("  foo  "); // false

この関数は、codegen(コードを用いてコードを生成すること)を行う際に有益なものです。なぜなら、有効な識別子を、ドット記法ブラケット記法の代わりに用いるなど、他の文字列プロパティとは異なる形で使用することができるからです。

js
const variables = ["foo", "foo:bar", "  foo  "];

function toAssignment(key) {
  if (isValidIdentifier(key)) {
    return `globalThis.${key} = undefined;`;
  }
  // JSON.stringify() escapes quotes and other special characters
  return `globalThis[${JSON.stringify(key)}] = undefined;`;
}

const statements = variables.map(toAssignment).join("\n");

console.log(statements);
// globalThis.foo = undefined;
// globalThis["foo:bar"] = undefined;
// globalThis["  foo  "] = undefined;

仕様書

Specification
ECMAScript® 2025 Language Specification
# prod-Assertion

ブラウザーの互換性

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
Input boundary assertion: ^, $

Legend

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

Full support
Full support

関連情報