Categories
JavaScript Answers

How to strip all punctuation from a string in JavaScript using regex?

Sometimes, we want to strip all punctuation from a string in JavaScript using regex.

In this article, we’ll look at how to strip all punctuation from a string in JavaScript using regex.

How to strip all punctuation from a string in JavaScript using regex?

To strip all punctuation from a string in JavaScript using regex, we call replace with a regex to replace all the punctuation marks from a string.

For instance, we write

const newStr = str.replace(/[^\w\s]|_/g, "").replace(/\s+/g, " ");

to call replace with /[^\w\s]|_/g to match all instances of non word characters and spaces and replace them with empty strings.

And we call replace again to replace all whitespaces with empty strings.

replace returns the string with the replacement done.

Conclusion

To strip all punctuation from a string in JavaScript using regex, we call replace with a regex to replace all the punctuation marks from a string.

Categories
JavaScript Answers

How to wait for a promise to finish before returning the variable of a function with JavaScript?

Sometimes, we want to wait for a promise to finish before returning the variable of a function with JavaScript.

In this article, we’ll look at how to wait for a promise to finish before returning the variable of a function with JavaScript.

How to wait for a promise to finish before returning the variable of a function with JavaScript?

To wait for a promise to finish before returning the variable of a function with JavaScript, we can use await.

For instance, we write

const waitForPromise = async () => {
  let result = await Promise.resolve("hello");
};

to create the waitForPromise function that calls Promise.resolve to return a promise.

Then we use await to wait for the result of the promise and get the result before we continue running the function.

Conclusion

To wait for a promise to finish before returning the variable of a function with JavaScript, we can use await.

Categories
JavaScript Answers

How to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript?

Sometimes, we want to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript.

In this article, we’ll look at how to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript.

How to suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript?

To suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript, we can switch on consent mode.

To do this, we write

gtag("consent", "default", {
  analytics_storage: "denied",
  ad_storage: "denied",
});

to call gtag with 'consent' and an object that denies cookies on users that haven’t consented to tracking with

{
  analytics_storage: "denied",
  ad_storage: "denied",
}

Conclusion

To suppress use of cookies for users who have not yet given consent with Google Analytics and JavaScript, we can switch on consent mode.

Categories
CSS

How to disable browser print options (headers, footers, margins) from page with CSS?

Sometimes, we want to disable browser print options (headers, footers, margins) from page with CSS

In this article, we’ll look at how to disable browser print options (headers, footers, margins) from page with CSS.

How to disable browser print options (headers, footers, margins) from page with CSS?

To disable browser print options (headers, footers, margins) from page with CSS, we can set print styles with the @page directive.

For instance, we write

@page {
  size: auto;
  margin: 0mm;
}

html {
  background-color: #ffffff;
  margin: 0px;
}

body {
  border: solid 1px blue;
  margin: 10mm;
}

to set the page size to auto and set margin to 0mm when the page is printed with

@page {
  size: auto;
  margin: 0mm;
}

Then we set styles for the html and body element that applies everywhere below that.

Conclusion

To disable browser print options (headers, footers, margins) from page with CSS, we can set print styles with the @page directive.

Categories
JavaScript Answers

How to check whether a CSS3 transition of an element has started or end with JavaScript?

Sometimes, we want to check whether a CSS3 transition of an element has started or end with JavaScript.

In this article, we’ll look at how to check whether a CSS3 transition of an element has started or end with JavaScript.

How to check whether a CSS3 transition of an element has started or end with JavaScript?

To check whether a CSS3 transition of an element has started or end with JavaScript, we listen to the transitionend event.

For instance, we write

element.addEventListener("transitionend", callback, false);

to listen to the transitionend event emitted by element with addEventListener

callback is called when the transition is finished.

Conclusion

To check whether a CSS3 transition of an element has started or end with JavaScript, we listen to the transitionend event.