title: XPath attribute quoting in JavaScript
body: <p>I am trying to build a function that will properly quote/escape an attribute in XPath.  I have seen solutions posted in C# <a href="https://stackoverflow.com/questions/642125/encoding-xpath-expressions-with-both-single-and-double-quotes/656016#656016">here</a> and <a href="https://stackoverflow.com/questions/642125/encoding-xpath-expressions-with-both-single-and-double-quotes/941723#941723">here</a>, but my implementation in JavaScript results in an error "This expression is not a legal expression"</p>
  
  <p>Here is my function:</p>
  
  <pre><code>function parseXPathAttribute(original){
              let result = null;
              /* If there are no double quotes, wrap in double quotes */
              if(original.indexOf("\"")&lt;0){
                  result = "\""+original+"\"";
              }else{
                  /* If there are no single quotes, wrap in single quotes */
                  if(original.indexOf("'")&lt;0){
                      result = "'"+original+"'";
                  }else{ /*Otherwise, we must use concat() */
                      result = original.split("\"")
                      for (let x = 0;x&lt;result.length;x++){
                          result[x] = result[x].replace(/"/g,"\\\"");
                          if (x&gt;0){
                              result[x] = "\\\""+result[x];
                          }
                          result[x] = "\""+result[x]+"\"";
                      }
                      result = result.join();
                      result = "concat("+result+")";
                  }
  
              }
  
              return result;
          }
  </code></pre>
  
  <p>Sample failing input:</p>
  
  <blockquote>
    <p>"'hi'"</p>
  </blockquote>
  
  <p>Sample failing output:</p>
  
  <blockquote>
    <p>concat("","\"'hi'","\"")]</p>
  </blockquote>
  
  <p>I don't understand why it is an illegal expression (given that the double quotes are escaped), so I don't know how to fix the function.</p>