Closed thekid closed 2 years ago
Input:
if ($greeting ~ '/^H[ea]llo/') { // Matches! } if ($matches= $greeting ~ '/^H[ea]llo/') { // Work with $matches }
Rewritten to:
if (preg_match($greeting, '/^H[ea]llo/')) { // Matches! } if (preg_match($greeting, '/^H[ea]llo/', $matches)) { // Work with $matches }
if (/^H[ea]llo/->matches($greeting)) { // Matches! }
if (Pattern::compile('/^H[ea]llo/')->matches($greeting)) { // Matches! }
Inspired by JavaScript template literals:
if (r`/^H[ea]llo/`($greeting)) { // Matches! }
if (preg_match('/^H[ea]llo/', $greeting)) { // Matches! }
=~
!~
First idea implemented as plugin -> https://github.com/xp-lang/php-pattern-operator
Idea 1: Tilde operator
Input:
Rewritten to:
Idea 2: Regex literal
Input:
Rewritten to:
Idea 3: Tagged template literal
Inspired by JavaScript template literals:
Input:
Rewritten to:
Prior art
=~
and!~
)