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.

Categories
JavaScript Answers

How to attach an click event handler to all links with JavaScript?

Sometimes, we want to attach an click event handler to all links with JavaScript.

In this article, we’ll look at how to attach an click event handler to all links with JavaScript.

How to attach an click event handler to all links with JavaScript?

To attach an click event handler to all links with JavaScript, we can use event delegation.

For instance, we write:

<a>foo</a>
<a>bar</a>
<p>
  baz
</p>

to add some elements.

Then we write:

window.onclick = (e) => {
  if (e.target.tagName !== 'A') {
    return
  }
  console.log('clicked a')
}

to set the window.onclick property to a click event handler function.

In it, we check if tagName of the clicked element in the window is 'A'.

If it’s not, we stop running the function.

Otherwise, we run console.log.

Therefore, when we clicked an a element, we see 'clicked a' logged.

Conclusion

To attach an click event handler to all links with JavaScript, we can use event delegation.

Categories
JavaScript Answers

How to add the JavaScript equivalent of Python’s rsplit method?

Sometimes, we want to add the JavaScript equivalent of Python’s rsplit method.

In this article, we’ll look at how to add the JavaScript equivalent of Python’s rsplit method.

How to add the JavaScript equivalent of Python’s rsplit method?

To add the JavaScript equivalent of Python’s rsplit method, we can create our own function.

Fir instance, we write:

const rsplit = (str, sep, maxsplit) => {
  const split = str.split(sep);
  return maxsplit ? [split.slice(0, -maxsplit).join(sep)].concat(split.slice(-maxsplit)) : split;
}

const a = rsplit("blah,derp,blah,beep", ",", 1)
console.log(a)

to create the rsplit function.

In it, we call str.split with the sep separator to split the string into an array.

Then we combine the string with split.slice(0, -maxsplit).join(sep) concatenated with split.slice(-maxsplit) to return an array with the combined and split string.

If maxsplit isn’t set, then we return the split array as is.

Therefore, we get ['blah,derp,blah', 'beep'] from the console log.

Conclusion

To add the JavaScript equivalent of Python’s rsplit method, we can create our own function.

Categories
JavaScript Answers

How to return a date in UTC with Moment.js?

Sometimes, we want to return a date in UTC with Moment.js.

In this article, we’ll look at how to return a date in UTC with Moment.js.

How to return a date in UTC with Moment.js?

To return a date in UTC with Moment.js, we can use the moment.utc method.

For instance, we write:

const d = moment.utc('2022-10-29T00:00:00.000')
console.log(d)

to call moment.utc with the date string we want to parse.

As a result, d is a moment object with the date time in UTC.

Conclusion

To return a date in UTC with Moment.js, we can use the moment.utc method.