Categories
JavaScript Answers

How to cast a string to an array with JavaScript?

Sometimes, we want to cast a string to an array with JavaScript.

In this article, we’ll look at how to cast a string to an array with JavaScript.

How to cast a string to an array with JavaScript?

To cast a string to an array with JavaScript, we can use the spread operator.

For instance, we write:

const s = 'foo'
const arr = [...s]
console.log(arr)

to spread the characters of s into an array with the spread operator and assign the array to arr.

Therefore, arr is ['f', 'o', 'o'].

Conclusion

To cast a string to an array with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to use a regex to get first word after slash in URL with JavaScript?

Sometimes, we want to use a regex to get first word after slash in URL with JavaScript.

In this article, we’ll look at how to use a regex to get first word after slash in URL with JavaScript.

How to use a regex to get first word after slash in URL with JavaScript?

To use a regex to get first word after slash in URL with JavaScript, we can call the string’s replace method.

For instance, we write:

const s = window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
console.log(s)

to call replace on the current URL with a regex that gets the first word after the slash and returns it.

Conclusion

To use a regex to get first word after slash in URL with JavaScript, we can call the string’s replace method.

Categories
JavaScript Answers

How to check the format of date with moment.js and JavaScript?

Sometimes, we want to check the format of date with moment.js and JavaScript.

In this article, we’ll look at how to check the format of date with moment.js and JavaScript.

How to check the format of date with moment.js and JavaScript?

To check the format of date with moment.js and JavaScript, we can use the moment.js’ isValid method.

For instance, we write:

const isValid = (d) => moment(d, 'DD-MMM-YYYY HH:mm a', true).isValid()

console.log(isValid('02-Mar-2022 01:01 AM'))
console.log(isValid('2022-02-03 01:01 AM'))

We define the isValid function that calls moment to try to parse the datetime string in 'DD-MMM-YYYY HH:mm a' format.

Then we call isValid to check if the datetime string is valid in the given format.

Therefore, datetimes that starts with a date, month and year separated by dashes and hours, minutes and AM or PM are valid.

As a result, the first console log is true and the 2nd one is false.

Conclusion

To check the format of date with moment.js and JavaScript, we can use the moment.js’ isValid method.

Categories
JavaScript Answers

How to detect whether a string is in URL format using JavaScript?

Sometimes, we want to detect whether a string is in URL format using JavaScript.

In this article, we’ll look at how to detect whether a string is in URL format using JavaScript.

How to detect whether a string is in URL format using JavaScript?

To detect whether a string is in URL format using JavaScript, we can use a regex.

For instance, we write:

const isUrl = (s) => {
  const regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  return regexp.test(s);
}

console.log(isUrl('http://example.com'))
console.log(isUrl('abc'))

to define the isUrl function to check if the string s matches the /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ regex with test.

We check if s starts with 'ftp', 'http', or 'https'.

And then we check if there’re colon, 2 slashes, 'www' and characters after that.

Therefore, we see true and false logged.

Conclusion

To detect whether a string is in URL format using JavaScript, we can use a regex.

Categories
JavaScript Answers

How to create a 2D array of zeroes in JavaScript?

Sometimes, we want to create a 2D array of zeroes in JavaScript.

In this article, we’ll look at how to create a 2D array of zeroes in JavaScript.

How to create a 2D array of zeroes in JavaScript?

To create a 2D array of zeroes in JavaScript, we can use some array methods.

For instance, we write:

const zeroes = Array(3).fill().map(() => Array(3).fill(0));
console.log(zeroes)

to create an array with Array to create an array with length 3.

Then we call fill and map to fill the empty slots and map them to arrays with 0’s in it.

Therefore, zeroes is:

[
  [
    0,
    0,
    0
  ],
  [
    0,
    0,
    0
  ],
  [
    0,
    0,
    0
  ]
]

Conclusion

To create a 2D array of zeroes in JavaScript, we can use some array methods.