Skip to main content
DevTools24

Regexエスケープ

特殊regex文字をエスケープしてリテラルにマッチさせます。置換文字列のエスケープも処理。

For replacement strings, only $ needs escaping (as $$)

.Any character
*0 or more
+1 or more
?0 or 1
^Start of string
$End of string
{Quantifier start
}Quantifier end
(Group start
)Group end
[Class start
]Class end
|Alternation
\Escape character

Regex Special Characters - 技術的な詳細

Special regex characters like . * + ? ^ $ { } ( ) | [ ] \\ have special meanings. To match them literally, prefix with a backslash. For replacement strings, $ needs to be escaped as $$.

コマンドラインでの代替方法

// JavaScript regex escape function
function escapeRegex(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

参照

公式仕様を見る