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 […]