/*!
 * jQuery replaceText
 *
 * Copyright 2015 Christoph Burschka <christoph@burschka.de>
 * This program is distributed under the terms of the MIT license.
 * Please see the LICENSE file for details.
 */
(function($){
  /**
   * Replace substrings with HTML.
   *
   * @param search A string or regular expression.
   * @param replace A string, or a DOM node, or a jQuery object,
   *                or an array of any of the above,
   *                or a function that returns any of the above.
   *                The function will receive the group captures as arguments.
   * @return the original jQuery object.
   */
  $.fn.replaceText = function(search, replace) {
    // This will be /undefined|/ for strings, with 0 groups.
    var capturing = RegExp(search.source + '|').exec('').length - 1;
    // Convert a static replacement value into a function that returns it.
    var rep = (typeof replace === 'function') ? replace : function() { return replace; };

    return this.each(function() {
      var remove = [];
      for (var node = this.firstChild; node; node = node.nextSibling)
        if (node.nodeType == document.TEXT_NODE)
          if (textNode(node, search, rep, capturing, !$.isFunction(replace)))
            remove.push(node);
      $(remove).remove();
    });
  }

  /**
   * Helper function for processing a text node.
   *
   * The new content is inserted into the node's parent just before the node,
   * leaving everything after it unchanged to preserve the sibling traversal.
   *
   * @param node The text node being processed.
   * @param search The original search argument.
   * @param replace The replacing function.
   * @param capturing The number of capturing groups of the search pattern.
   * @param clone Whether the replaced elements must be cloned before insertion.
   * @return true or false, depending on whether any matches were found.
   */
  function textNode(node, search, replace, capturing, clone) {
    var tokens = node.nodeValue.split(search);
    if (tokens.length < 2) return false;

    // Render the matches and concatenate everything.
    var output = [];
    for (var i = 0; i+capturing+1 < tokens.length; i += capturing+1) {
      var child = replace.apply(this, tokens.slice(i+1, i+1+capturing)) || '';
      output = output.concat(tokens[i], child);
    }
    output.push(tokens[tokens.length-1]);

    // Combine runs of strings into text nodes.
    var nodes = [];
    var text = '';
    for (var i in output) {
      if ($.type(output[i]) === 'string') text += output[i];
      else {
        if (text) nodes.push(document.createTextNode(text));
        nodes.push(clone ? output[i].clone(true) : output[i]);
        text = '';
      }
    }
    if (text) nodes.push(document.createTextNode(text));

    // Prepend all nodes to the given node, which will be marked for removal.
    return $(node).before(nodes) && true;
  };
})(jQuery);

@@ -736,21 +736,19 @@
     
-var capturing
+const width
  = R
@@ -874,19 +874,21 @@
 it.%0A    
-var
+const
  rep = (
@@ -1003,19 +1003,21 @@
 %7B%0A      
-var
+const
  remove 
@@ -1033,19 +1033,19 @@
    for (
-var
+let
  node = 
@@ -1183,25 +1183,21 @@
 h, rep, 
-capturing
+width
 , !$.isF
@@ -1659,25 +1659,21 @@
  @param 
-capturing
+width
  The num
@@ -1922,25 +1922,21 @@
 eplace, 
-capturing
+width
 , clone)
@@ -1942,19 +1942,21 @@
 ) %7B%0A    
-var
+const
  tokens 
@@ -2087,19 +2087,19 @@
 ng.%0A    
-var
+let
  output 
@@ -2113,19 +2113,19 @@
    for (
-var
+let
  i = 0; 
@@ -2126,25 +2126,21 @@
  = 0; i+
-capturing
+width
 +1 %3C tok
@@ -2156,25 +2156,21 @@
 h; i += 
-capturing
+width
 +1) %7B%0A  
@@ -2173,19 +2173,21 @@
 %7B%0A      
-var
+const
  child =
@@ -2229,25 +2229,21 @@
 +1, i+1+
-capturing
+width
 )) %7C%7C ''
@@ -2393,19 +2393,21 @@
 es.%0A    
-var
+const
  nodes =
@@ -2415,19 +2415,19 @@
 %5B%5D;%0A    
-var
+let
  text = 
@@ -2443,16 +2443,19 @@
 or (
-var i in
+let item of
  out
@@ -2478,25 +2478,20 @@
 ($.type(
-output%5Bi%5D
+item
 ) === 's
@@ -2506,25 +2506,20 @@
 text += 
-output%5Bi%5D
+item
 ;%0A      
@@ -2613,25 +2613,20 @@
 clone ? 
-output%5Bi%5D
+item
 .clone(t
@@ -2636,17 +2636,12 @@
 ) : 
-output%5Bi%5D
+item
 );%0A 

Convert to ES2015 style.