Categories
JavaScript Answers

How to check for special characters in string with JavaScript?

Sometimes, we want to check for special characters in string with JavaScript.

In this article, we’ll look at how to check for special characters in string with JavaScript.

How to check for special characters in string with JavaScript?

To check for special characters in string with JavaScript, we can use the regex test method.

For instance, we write

const format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
const hasSpecialChars = format.test(string);

to define the format regex that checks for the existence of any special characters listed in the square brackets.

Then we call format.test with string to check if string has the characters listed in format.

Conclusion

To check for special characters in string with JavaScript, we can use the regex test method.

Categories
React Answers

How to handle 401 error in Axios and React?

Sometimes, we want to handle 401 error in Axios and React.

In this article, we’ll look at how to handle 401 error in Axios and React.

How to handle 401 error in Axios and React?

To handle 401 error in Axios and React, we can add a response interceptor.

For instance, we write

axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    if (error.response.status === 401) {
      // ...
    }
    return error;
  }
);

to call axios.interceptors.response.use with a function that runs when we get a response and a function that runs when we get an error respectively.

In the error handling function, we get the response status code with error.response.status.

If it’s 401, then we do some thing with the response.

Conclusion

To handle 401 error in Axios and React, we can add a response interceptor.

Categories
JavaScript Answers

How to get a range of items from a JavaScript array?

Sometimes, we want to get a range of items from a JavaScript array.

In this article, we’ll look at how to get a range of items from a JavaScript array.

How to get a range of items from a JavaScript array?

To get a range of items from a JavaScript array, we can use the array slice method.

For instance, we write

const fruits = ["apple", "banana", "peach", "plum", "pear"];
const slice1 = fruits.slice(1, 3);
const slice2 = fruits.slice(3);

to call fruits.slice with 1 and 3 to return an array from fruits at index 1 and 2.

Next, we call slice with 3 to return an array from fruits at index 3 to the end of the array.

Therefore, slice1 is ['banana', 'peach'].

And slice2 is ['plum', 'pear'].

Conclusion

To get a range of items from a JavaScript array, we can use the array slice method.

Categories
JavaScript Answers

How to fix moment.js isValid function not working properly with JavaScript?

To fix moment.js isValid function not working properly with JavaScript, we can enable strict validation.

For instance, we write

const isValid = moment("It is 2022-05-25", "YYYY-MM-DD").isValid();
const isValid2 = moment("It is 2022-05-25", "YYYY-MM-DD", true).isValid();

to call moment with true as the 3rd argument so that isValid will make sure the date string in the first argument of moment exactly follows the format specified in the string in the 2nd argument.

Therefore, isValid is true, but isValid2 is false since the date string doesn’t exactly follow the YYYY-MM-DD format.

Categories
JavaScript Answers

How to pause setInterval() functions with JavaScript?

Sometimes, we want to pause setInterval() functions with JavaScript.

In this article, we’ll look at how to pause setInterval() functions with JavaScript.

How to pause setInterval() functions with JavaScript?

To pause setInterval() functions with JavaScript, we call the clearInterval function.

For instance, we write

let doThis = null;

const y = () => {
  //...
};

doThis = setInterval(y, 1000);

const yStart = () => {
  doThis = setInterval(y, 1000);
};

const yStop = () => {
  clearInterval(doThis);
};

to define the yStart function.

In it, we call setInterval with y and 1000 to call y every second.

We assign the returned timer ID to the doThis variable.

Then we define the yStop function that calls clearInterval with doThis to stop the timer.

Conclusion

To pause setInterval() functions with JavaScript, we call the clearInterval function.