private void applyReplacementHighlighting(JTextComponent comp, Pattern pattern, String repString) {
    Matcher matcher = pattern.matcher(comp.getText());
    int charsRemoved = 0;
    
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();
    
    comp.setText(comp.getText().replaceAll(pattern.pattern(), repString));
    
    while(matcher.find()) {
      try {
        int matchLength = matcher.end() - matcher.start();
        int numRemoved = matchLength - repString.length();
        
        h.addHighlight(matcher.start()-charsRemoved, 
                      matcher.start()-charsRemoved+repString.length(), 
                      regexPainter);
                      
        charsRemoved += numRemoved;
      }
      catch(Exception e) {
        throw new RuntimeException(e);
      }
    }
  }