Search code examples
javascriptnode.jsreadme

Javascript single quote inside string for exported text file


I am creating a node.js file that creates and readme file where the install directions need to be displayed as shell or javascript type font. However, I have not been able to use the single quotes required to make that work since they close out the string.

function generateExport(data) {
  const { username, ...title} = data;

  return `
    ```shell <--- how would you do this ?
    ${data.title} 
    ```  <----- and this?
`;
}

Solution

  • Escape the string with a backslash before each one

    function generateExport(data) {
      const { username, ...title} = data;
    
      return `
        \`\`\`shell <--- how would you do this ?
        ${data.title} 
        \`\`\`  <----- and this?
    `;
    }