/*!
 * 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.
    const width = RegExp(search.source + '|').exec('').length - 1;
    // Convert a static replacement value into a function that returns it.
    const rep = $.isFunction(replace) ? replace : () => replace;
    const clone = !$.isFunction(replace);

    return this.each(function () {
      const remove = [];
      for (let node = this.firstChild; node; node = node.nextSibling) {
        if (node.nodeType == document.TEXT_NODE) {
          textNode(node, search, rep, width, clone) && 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 width 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, width, clone) {
    const tokens = node.nodeValue.split(search);
    if (tokens.length < 2) return false;

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

    // Combine runs of strings into text nodes.
    const nodes = [];
    let text = '';
    output.forEach(item => {
      if ($.type(item) !== 'string') {
        if (text) nodes.push(document.createTextNode(text));
        nodes.push(clone ? item.clone(true) : item);
        text = '';
      } else text += item;
    });
    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);

@@ -987,321 +987,8 @@
 %0A%0A  
-  return this.each(function () %7B%0A      const remove = %5B%5D;%0A      for (let node = this.firstChild; node; node = node.nextSibling) %7B%0A        if (node.nodeType == document.TEXT_NODE) %7B%0A          textNode(node, search, rep, width, clone) && remove.push(node);%0A        %7D%0A      %7D%0A      $(remove).remove();%0A    %7D);%0A  %7D;%0A%0A
   /*
@@ -992,16 +992,18 @@
  /**%0A   
+ 
+ 
 * Helper
@@ -1043,21 +1043,25 @@
 ode.%0A   
+  
 *%0A   
+ 
+ 
 * The ne
@@ -1130,16 +1130,18 @@
 ode,%0A   
+ 
+ 
 * leavin
@@ -1206,26 +1206,30 @@
 aversal.%0A   
+ 
+ 
 *%0A
+  
    * @param 
@@ -1270,252 +1270,8 @@
 .%0A  
- * @param search The original search argument.%0A   * @param replace The replacing function.%0A   * @param width The number of capturing groups of the search pattern.%0A   * @param clone Whether the replaced elements must be cloned before insertion.%0A
    *
@@ -1346,21 +1346,22 @@
 %0A   
+  
 */%0A  
-function
+  const
  tex
@@ -1369,48 +1369,23 @@
 Node
-(
+ = 
 node
-, search, replace, width, clone) %7B%0A
+ =%3E %7B%0A  
     
@@ -1429,16 +1429,18 @@
 earch);%0A
+  
     if (
@@ -1477,16 +1477,18 @@
 e;%0A%0A    
+  
 // Rende
@@ -1529,16 +1529,18 @@
 ything.%0A
+  
     let 
@@ -1556,16 +1556,18 @@
 %5B%5D;%0A    
+  
 for (let
@@ -1625,24 +1625,26 @@
  1) %7B%0A      
+ 
+ 
 const child 
@@ -1648,20 +1648,16 @@
 ld = rep
-lace
 .apply(t
@@ -1706,24 +1706,26 @@
 %7C '';%0A      
+  
 output = out
@@ -1762,10 +1762,14 @@
     
+ 
+ 
 %7D%0A
+  
     
@@ -1809,24 +1809,26 @@
 - 1%5D);%0A%0A    
+ 
+ 
 // Combine r
@@ -1859,16 +1859,18 @@
  nodes.%0A
+  
     cons
@@ -1883,24 +1883,26 @@
 s = %5B%5D;%0A    
+  
 let text = '
@@ -1900,24 +1900,26 @@
  text = '';%0A
+  
     output.f
@@ -1937,24 +1937,26 @@
  =%3E %7B%0A      
+  
 if ($.type(i
@@ -1972,24 +1972,26 @@
 'string') %7B%0A
+  
         if (
@@ -2047,16 +2047,18 @@
         
+  
 nodes.pu
@@ -2102,16 +2102,18 @@
         
+  
 text = '
@@ -2111,24 +2111,26 @@
  text = '';%0A
+  
       %7D else
@@ -2152,12 +2152,16 @@
     
+ 
+ 
 %7D);%0A
+  
     
@@ -2218,16 +2218,18 @@
 );%0A%0A    
+ 
+ 
 // Prepe
@@ -2298,16 +2298,18 @@
 al.%0A    
+  
 return $
@@ -2338,16 +2338,306 @@
 & true;%0A
+    %7D;%0A%0A    return this.each(function () %7B%0A      const remove = %5B%5D;%0A      for (let node = this.firstChild; node; node = node.nextSibling) %7B%0A        if (node.nodeType == document.TEXT_NODE && textNode(node)) %7B%0A          remove.push(node);%0A        %7D%0A      %7D%0A      $(remove).remove();%0A    %7D);%0A
   %7D;%0A%7D)(

Move textNode inside replaceText scope.