Categories
JavaScript Answers

How to remove all script tags from HTML with a JavaScript regular expression?

Sometimes, we want to remove all script tags from HTML with a JavaScript regular expression.

In this article, we’ll look at how to remove all script tags from HTML with a JavaScript regular expression.

How to removing all script tags from HTML with a JavaScript regular expression?

To remove all script tags from HTML with a JavaScript regular expression, we can use a regex that match the script tags and its contents.

For instance, we write

/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi

to define a regex that matches the script tags and the contents inside.

We use g to find all matches.

And we use i to find matches in a case insensitive manner.

Conclusion

To remove all script tags from HTML with a JavaScript regular expression, we can use a regex that match the script tags and its contents.

Categories
JavaScript Answers

How to filter a JavaScript Map?

Sometimes, we want to filter a JavaScript Map.

In this article, we’ll look at how to filter a JavaScript Map.

How to filter a JavaScript Map?

To filter a JavaScript Map, we convert it to an array.

For instance, we write

const map0 = new Map([
  ["a", 1],
  ["b", 2],
  ["c", 3],
]);

const map1 = new Map([...map0].filter(([k, v]) => v < 3));

console.info([...map1]);

to create a map with the Map constructor.

Then we convert it to an array with the spread operator.

And then we call filter with a function that checks if value v is less than 3.

We use the returned array to create another Map.

Conclusion

To filter a JavaScript Map, we convert it to an array.

Categories
JavaScript Answers

How to create an associative array in JavaScript literal notation?

Sometimes, we want to create an associative array in JavaScript literal notation.

In this article, we’ll look at how to create an associative array in JavaScript literal notation.

How to create an associative array in JavaScript literal notation?

To create an associative array in JavaScript literal notation, we can use maps.

For instance, we write

const arr = new Map([
  ["key1", "User"],
  ["key2", "Guest"],
  ["key3", "Admin"],
]);

const res = arr.get("key2");
console.log(res);

to create a map by calling the Map constructor with a nested array of key-value pair arrays.

Then we calk get to get the value with the key 'key2'.

So res is 'Guest'.

Conclusion

To create an associative array in JavaScript literal notation, we can use maps.

Categories
JavaScript Answers

How to fix JSON.stringify not working with normal JavaScript array?

Sometimes, we want to fix JSON.stringify not working with normal JavaScript array.

In this article, we’ll look at how to fix JSON.stringify not working with normal JavaScript array.

How to fix JSON.stringify not working with normal JavaScript array?

To fix JSON.stringify not working with normal JavaScript array, we should make sure arrays have all numerical keys.

For instance, we write

const test = {};
test.a = "test";
test.b = [];
test.b.push("item");
test.b.push("item2");
test.b.push("item3");
const json = JSON.stringify(test);
console.log(json);

to create array properties a and b.

And we call push to push contents into it.

Then we call JSON.stringify to return the test object converted to a JSON string.

Conclusion

To fix JSON.stringify not working with normal JavaScript array, we should make sure arrays have all numerical keys.

Categories
JavaScript Answers

How to insert a large block of HTML in JavaScript?

Sometimes, we want to insert a large block of HTML in JavaScript.

In this article, we’ll look at how to insert a large block of HTML in JavaScript.

How to insert a large block of HTML in JavaScript?

To insert a large block of HTML in JavaScript, we use template literals.

For instance, we write

const a = 1;
const b = 2;
const div = document.createElement("div");
div.setAttribute("class", "post block bc2");
div.innerHTML = `
    <div class="parent">
        <div class="child">${a}</div>
        <div class="child">+</div>
        <div class="child">${b}</div>
        <div class="child">=</div>
        <div class="child">${a + b}</div>
    </div>
`;
document.getElementById("posts").appendChild(div);

to create the div with createElement.

Then we set its class attribute with setAttribute.

Next we set its content by setting the innerHTML property to the HTML we want to render.

We use template literals to interpolate values in the HTML.

And then we call appendChild to append the div as the last child of the element with ID posts.

Conclusion

To insert a large block of HTML in JavaScript, we use template literals.