code: public static String escapeJavaScriptForChain(String javaScript)
      {
          // first replace \' with \\'
          //String escaped = StringUtils.replace(javaScript, "\\'", "\\\\'");
  
          // then replace ' with \'
          // (this will replace every \' in the original to \\\')
          //escaped = StringUtils.replace(escaped, '\'', "\\'");
  
          //return escaped;
  
          StringBuilder out = null;
          for (int pos = 0; pos < javaScript.length(); pos++)
          {
              char c = javaScript.charAt(pos);
  
              if (c == '\\' || c == '\'')
              {
                  if (out == null)
                  {
                      out = new StringBuilder(javaScript.length() + 8);
                      if (pos > 0)
                      {
                          out.append(javaScript, 0, pos);
                      }
                  }
                  out.append('\\');
              }
              if (out != null)
              {
                  out.append(c);
              }
          }
  
          if (out == null)
          {
              return javaScript;
          }
          else
          {
              return out.toString();
          }
      }
docs: This function correctly escapes the given JavaScript code
  for the use in the jsf.util.chain() JavaScript function.
  It also handles double-escaping correclty.
  
  @param javaScript
  @return