// What must be escaped in the route regex
const REGEX_ESCAPE = '[.\\+*?[^\\]${}=!|]';
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
This part of code means, that all chars (except round and angular brackets) will be escaped. It helps to detect question mark or dot in specific route.
\\\\$0
To use backslash you need to duplicate it in your regexpr.
Few examples of result using this preg_replace:
test => test
test/ => test/
//test/ => //test/
//test/! => //test/#!
//test/!#$ => //test/!#\$
//test/!#$%^&*aaa()bbb => //test/!#\$%\^&*aaa()bbb