'use strict';

const _specialCharacters = [
  {
    chr: '\\',
    regexp: /\\/g
  }, {
    chr: '^',
    regexp: /\^/g
  }, {
    chr: '$',
    regexp: /\$/g
  }, {
    chr: '*',
    regexp: /\*/g
  }, {
    chr: '+',
    regexp: /\+/g
  }, {
    chr: '?',
    regexp: /\?/g
  }, {
    chr: '.',
    regexp: /\./g
  }
];

const transformSpecialCharacters = function(token) {
  return _specialCharacters.reduce((transformedToken, special) => {
    return transformedToken.replace(special.regexp, '\\' + special.chr);
  }, token);
};

module.exports = transformSpecialCharacters;

@@ -18,514 +18,101 @@
 nst 
-_specialCharacters = %5B%0A  %7B%0A    chr: '%5C%5C',%0A    regexp: /%5C%5C/g%0A  %7D, %7B%0A    chr: '%5E',%0A    regexp: /%5C%5E/g%0A  %7D, %7B%0A    chr: '$',%0A    regexp: /%5C$/g%0A  %7D, %7B%0A    chr: '*',%0A    regexp: /%5C*/g%0A  %7D, %7B%0A    chr: '+',%0A    regexp: /%5C+/g%0A  %7D, %7B%0A    chr: '?',%0A    regexp: /%5C?/g%0A  %7D, %7B%0A    chr: '.',%0A    regexp: /%5C./g%0A  %7D%0A%5D;%0A%0Aconst transformSpecialCharacters = function(token) %7B%0A  return _specialCharacters.reduce((transformedToken, special) =%3E %7B%0A    return transformedToken.replace(special.regexp, '%5C%5C' + special.chr);%0A  %7D, token
+transformSpecialCharacters = function(token) %7B%0A  return token.replace(/(%5B%5C%5C%5E$*+?.%5D)/g, '%5C%5C$1'
 );%0A%7D

use regular expression instead of special character array