title: A PHP script for MongoDB accent search needs to translate into JavaScript
body: <p>I found a PHP script that let to find text in some accent characters. My project is nodejs+mongodb so I tried to translate JavaScript but I couldn't be able to translate it at all. Since I don't know PHP very well, need some help to translate. </p>
  
  <p>PHP Script source code is from <a href="http://tech.rgou.net/en/php/pesquisas-nao-sensiveis-ao-caso-e-acento-no-mongodb-e-php/" rel="nofollow noreferrer">http://tech.rgou.net/en/php/pesquisas-nao-sensiveis-ao-caso-e-acento-no-mongodb-e-php/</a></p>
  
  <pre><code>/**
  
  * Description of StringUtil
  
  *
  
  * @author  Rafael Goulart
  
  */
  
  class StringUtil {
  
  const ACCENT_STRINGS = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËẼÌÍÎÏĨÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëẽìíîïĩðñòóôõöøùúûüýÿ';
  
  const NO_ACCENT_STRINGS = 'SOZsozYYuAAAAAAACEEEEEIIIIIDNOOOOOOUUUUYsaaaaaaaceeeeeiiiiionoooooouuuuyy';
  
  /**
  
  * Returns a string with accent to REGEX expression to find any combinations
  
  * in accent insentive way
  
  *
  
  * @param string $text The text.
  
  * @return string The REGEX text.
  
  */
  
  static public function accentToRegex($text)
  
  {
  
  $from = str_split(utf8_decode(self::ACCENT_STRINGS));
  
  $to   = str_split(strtolower(self::NO_ACCENT_STRINGS));
  
  $text = utf8_decode($text);
  
  $regex = array();
  
  foreach ($to as $key =&gt; $value)
  
  {
  
  if (isset($regex[$value]))
  
  {
  
  $regex[$value] .= $from[$key];
  
  } else {
  
  $regex[$value] = $value;
  
  }
  
  }
  
  foreach ($regex as $rg_key =&gt; $rg)
  
  {
  
  $text = preg_replace("/[$rg]/", "_{$rg_key}_", $text);
  
  }
  
  foreach ($regex as $rg_key =&gt; $rg)
  
  {
  
  $text = preg_replace("/_{$rg_key}_/", "[$rg]", $text);
  
  }
  
  return utf8_encode($text);
  
  }
  
  }
  </code></pre>
  
  <p>And here is my JavaScript code that need to be translate ... Any help would be appricated! Thanks.</p>
  
  <pre><code>function accentToRegex(word){
  
      var ACCENT_STRINGS = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËẼÌÍÎÏĨÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëẽìíîïĩðñòóôõöøùúûüýÿ';
      var NO_ACCENT_STRINGS = 'SOZsozYYuAAAAAAACEEEEEIIIIIDNOOOOOOUUUUYsaaaaaaaceeeeeiiiiionoooooouuuuyy';
  
      var from =  (ACCENT_STRINGS).split(decode_utf8(ACCENT_STRINGS));
      var to   = NO_ACCENT_STRINGS.split(NO_ACCENT_STRINGS.toLocaleLowerCase());
  
      var text = decode_utf8(word);
      var regex = new Array();
  
      for(value in to)
      {
          if (!regex[value])
          {
              regex[value] = from[value];
          } else {
              regex[value] = value;
          }
      }
  
      for (rg_key in regex)
  
      {
  //          "$TESTONE $TESTONE".replace( new RegExp("\\$TESTONE","gm"),"foo")
  //        text = preg_replace("/[$rg]/", "_{$rg_key}_", $text);
          text = text.replace(new RegExp(/[rg]/), new RegExp(_{rg_key}_)) ;
  
      }
  
      foreach (rg in regex)
    {
  
  //        $text = preg_replace("/_{$rg_key}_/", "[$rg]", $text);
        text = text.replace(new RegExp(/[rg]/), new RegExp(_{rg_key}_)) ;
  
    }
  
      return encode_utf8(text); //Edited from $text to text
  
  }
  
  function encode_utf8(s) {
      return encodeURIComponent(s);
  }
  
  function decode_utf8(s) {
      return decodeURIComponent(s);
  }
  </code></pre>
  
  <p>The error is </p>
  
  <p><img src="https://i.stack.imgur.com/IOZnh.png" alt="enter image description here"></p>