diff --git a/advagg_js_minify/src/Asset/JsOptimizer.php b/advagg_js_minify/src/Asset/JsOptimizer.php
index f0c51f5..9a6fba6 100644
--- a/advagg_js_minify/src/Asset/JsOptimizer.php
+++ b/advagg_js_minify/src/Asset/JsOptimizer.php
@@ -215,11 +215,12 @@ class JsOptimizer implements AssetOptimizerInterface {
         return $data;
       }
 
-      // Under some unknown/rare circumstances, JSMin and JSqueeze can add a
-      // couple of extraneous/wrong chars at the end of the string. Check and
-      // remove if necessary.
-      if (substr(trim($data),-2) == "\xE3\x7F") {
-        $data = substr($data,0,-2);
+      // Under some unknown/rare circumstances, JSMin and JSqueeze can add 2-3
+      // extraneous/wrong chars at the end of the string. Check and remove if
+      // necessary. The first chars varies a bit but the last char is always an
+      // ASCII control character.
+      if (ctype_cntrl(substr(trim($data),-1))) {
+        $data = substr($data,0,strrpos($data,';'));
       }
 
       // Ensure that $data ends with ; or }.
