0

In my project I am using a lot of JavaScript template string. After webpack compilation that is resulting in newlines, tabs and white spaces. How can I remove it with regex or any DOM API like document.createTreeWalker? I hope there would be easy regex to replace these.

Here is my source

const employees = ['Rajkeshwar', 'Suresh', 'Manoj', 'Ramesh', 'Sudhir'];
const domStr = `
  <h1>Employee List</h1>
  <ul class="employee-list">
    ${employees.reduce((str, emp) => {
       str += `<li>${emp}</li>`;
       return str;
    }, '')}
  </ul>
`;

This is what I get after compilation


var employees = ["Rajkeshwar", "Suresh", "Manoj", "Ramesh", "Sudhir"];
var domStr = '\n  <h1>Employee List</h1>\n  <ul class="employee-list">\n    '.concat(
  employees.reduce(function(str, emp) {
    str += "<li>".concat(emp, "</li>");
    return str;
  }, ""),
  "\n  </ul>\n"
);

1 Answer 1

1

You can use the following way to concatenate the string .

const employees = ['Rajkeshwar', 'Suresh', 'Manoj', 'Ramesh', 'Sudhir'];
    var domStr = `
    <h1>Employee List</h1>
    <ul class="employee-list">`;
    employees.forEach(function(employee){
                    domStr += `<li>`+employee+`</li>`; 
    });
    domStr = domStr + `</ul>`;

In domStr you will have your string .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.