'Negation Regular Expression
I have a regular expression to minify the result of the code generated by Laravel's view compiler. The regular expression does nothing more than minify the HTML when compiling a view. I'm having trouble setting the regex to ignore attributes starting with ":" and "@" (eg ... @click="hide(true)" :class="{collapsed: !open}">), since alpinejs uses them.
In the HTML code:
<select
id="version-switcher"
:class="{test: true}"
aria-label="Localhost version"
class="appearance-none"
@change="window.location = $event.target.value"
>
<option value="https://localhost">Test</option>
<option selected value="https://localhost">Foo</option>
</select>
The result should be:
<select id="version-switcher" :class="{test: true}" aria-label="Localhost version" class="appearance-none" @change="window.location = $event.target.value"><option value="https://localhost">Test</option><option selected value="https://localhost">Foo</option></select>
However, the output is:
<select id="version-switcher":class="{test: true}" aria-label="Localhost version" class="appearance-none"@change="window.location = $event.target.value"><option value="https://localhost">Test</option><option selected value="https://localhost">Foo</option></select>
Note that the attribute starting with : and the one starting with @ are not separate from the previous attribute. The regular expression is: return preg_replace('/<!--(.*?)-->|\s\B/um', '', $html);
Can someone help me with this problem please?
Solution 1:[1]
You can use
preg_replace('~<!--[^-]*(?:-(?!->)[^-]*)*-->|\s+(?=\s[@:]?\w[\w-]*=|[<>])~u', '', $text)
See the regex demo.
Details:
<!--[^-]*(?:-(?!->)[^-]*)*-->-<!--string, then zero or more chars other than-, then zero or more repetitions of-not immediately followed with->and then zero or more non-hyphen chars|- or\s+- one or more whitespaces(?=\s[@:]?\w[\w-]*=|[<>])- that are immediately followed with\s[@:]?\w[\w-]*=- a whitespace, an optional@or:, a word char, zero or more word or-chars and then a=char|- or[<>]- a<or>char.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
