title: Regex ignore matches between <script> tags
body: <p>I apologise as I have very little knowledge about Regex and I don't even understand exactly what this regex is doing (I didn't write it - <a href="http://wordpress.org/support/topic/plugin-relevanssi-a-better-search-highlights-happening-inside-script-tags" rel="nofollow">source</a>) apart from the fact it searches for a certain term so that it can be highlighted.</p>
  
  <p>Here is the Regex:</p>
  
  <pre><code>/(\b$term|$term\b)(?!([^&lt;]+)?&gt;)/iu
  </code></pre>
  
  <p>The problem is I need to make sure it doesn't match anything between <code>&lt;script&gt;</code> and <code>&lt;/script&gt;</code> tags. Now I know there are many variations of how a script tag can be written but really all I need it to do is ignore any text between <code>&lt;script</code> and <code>/script&gt;</code> taking into account possible whitespace between <code>script</code> and <code>&lt;</code> like <code>&lt;  script</code> or <code>/script  &gt;</code>.</p>
  
  <p>Is anyone able to modify it in this way? I will notify the plugin's author who wrote this reg-ex for inclusion in future releases.</p>
  
  <p><strong>Edit:</strong> Here is the function it originates from:</p>
  
  <pre><code>function relevanssi_highlight_terms($excerpt, $query) {
      $type = get_option("relevanssi_highlight");
      if ("none" == $type) {
          return $excerpt;
      }
  
      switch ($type) {
          case "mark":                        // thanks to Jeff Byrnes
              $start_emp = "&lt;mark&gt;";
              $end_emp = "&lt;/mark&gt;";
              break;
          case "strong":
              $start_emp = "&lt;strong&gt;";
              $end_emp = "&lt;/strong&gt;";
              break;
          case "em":
              $start_emp = "&lt;em&gt;";
              $end_emp = "&lt;/em&gt;";
              break;
          case "col":
              $col = get_option("relevanssi_txt_col");
              if (!$col) $col = "#ff0000";
              $start_emp = "&lt;span style='color: $col'&gt;";
              $end_emp = "&lt;/span&gt;";
              break;
          case "bgcol":
              $col = get_option("relevanssi_bg_col");
              if (!$col) $col = "#ff0000";
              $start_emp = "&lt;span style='background-color: $col'&gt;";
              $end_emp = "&lt;/span&gt;";
              break;
          case "css":
              $css = get_option("relevanssi_css");
              if (!$css) $css = "color: #ff0000";
              $start_emp = "&lt;span style='$css'&gt;";
              $end_emp = "&lt;/span&gt;";
              break;
          case "class":
              $css = get_option("relevanssi_class");
              if (!$css) $css = "relevanssi-query-term";
              $start_emp = "&lt;span class='$css'&gt;";
              $end_emp = "&lt;/span&gt;";
              break;
          default:
              return $excerpt;
      }
  
      $start_emp_token = "*[/";
      $end_emp_token = "\]*";
  
      if ( function_exists('mb_internal_encoding') )
          mb_internal_encoding("UTF-8");
  
      $terms = array_keys(relevanssi_tokenize($query, $remove_stopwords = true));
  
      $phrases = relevanssi_extract_phrases(stripslashes($query));
  
      $non_phrase_terms = array();
      foreach ($phrases as $phrase) {
          $phrase_terms = array_keys(relevanssi_tokenize($phrase, false));
          foreach ($terms as $term) {
              if (!in_array($term, $phrase_terms)) {
                  $non_phrase_terms[] = $term;
              }
          }
          $terms = $non_phrase_terms;
          $terms[] = $phrase;
      }
  
      usort($terms, 'relevanssi_strlen_sort');
  
      get_option('relevanssi_word_boundaries', 'on') == 'on' ? $word_boundaries = true : $word_boundaries = false;
      foreach ($terms as $term) {
          $pr_term = preg_quote($term, '/');
          if ($word_boundaries) {
              $excerpt = preg_replace("/(\b$pr_term|$pr_term\b)(?!([^&lt;]+)?&gt;)/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt);
          }
          else {
              $excerpt = preg_replace("/($pr_term)(?!([^&lt;]+)?&gt;)/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt);
          }
          // thanks to http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/
      }
  
      $excerpt = relevanssi_remove_nested_highlights($excerpt, $start_emp_token, $end_emp_token);
  
      $excerpt = str_replace($start_emp_token, $start_emp, $excerpt);
      $excerpt = str_replace($end_emp_token, $end_emp, $excerpt);
      $excerpt = str_replace($end_emp . $start_emp, "", $excerpt);
      if (function_exists('mb_ereg_replace')) {
          $pattern = $end_emp . '\s*' . $start_emp;
          $excerpt = mb_ereg_replace($pattern, " ", $excerpt);
      }
  
      return $excerpt;
  }
  </code></pre>