Skip to content

Regular Expression – remove quotes from HTML attributes that does not contain spaces but keep quotes around URLs

I’m trying to simplify HTML and JS for my page, and have problem with regular expression:

$replace = [    
...,
'~([rnt ])?([a-zA-Z0-9]+)="([a-zA-Z0-9_/\-]+)"([rnt ])?~s' => '$1$2=$3'];

$scripts = preg_replace(array_keys($replace), array_values($replace), $scripts);
$html = preg_replace($searchh, $replacee, $html);

But in case of expression like this:

    <script>var swatchImageOption4 = document.querySelector('div[data-option-id="4"]');
swatchImageOption4.style.background = "u0023f70d0d no-repeat center";
swatchImageOption4.style.backgroundSize = "initial";</script> 

It deletes “” in data-option-id=”4″ and swatches on frontend doesnt work.
Any idea how can i change it?