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
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.

Categories
JavaScript Answers

How to load the contents of a text file into a JavaScript variable?

Sometimes, we want to load the contents of a text file into a JavaScript variable.

In this article, we’ll look at how to load the contents of a text file into a JavaScript variable.

How to load the contents of a text file into a JavaScript variable?

To load the contents of a text file into a JavaScript variable, we call text from the response object that we get with fetch.

For instance, we write

const load = async (url) => {
  try {
    const response = await fetch(url);
    const data = await response.text();
    console.log(data);
  } catch (err) {
    console.error(err);
  }
};

load("https://example.com");

to create the load function that calls fetch to make a GET request to the url.

Then we call response.text to get the response body as a text string.

Conclusion

To load the contents of a text file into a JavaScript variable, we call text from the response object that we get with fetch.