Categories
JavaScript Answers

How to match multiple words in regex with JavaScript?

Sometimes, we want to match multiple words in regex with JavaScript.

In this article, we’ll look at how to match multiple words in regex with JavaScript.

How to match multiple words in regex with JavaScript?

To match multiple words in regex with JavaScript, we can use lookahead assertions.

For instance, we write:

const r = /^(?=.*foo)(?=.*bar)(?=.*baz)/
console.log('foobarbazqux'.match(r))
console.log('foobar'.match(r))

to match the substrings containing foo, bar and baz.

The strings in the regex won’t be included in the match.

Therefore, the console logs log

[""]
null

Conclusion

To match multiple words in regex with JavaScript, we can use lookahead assertions.

Categories
JavaScript Answers

How to convert a date string to ISO date with JavaScript?

Sometimes, we want to convert a date string to ISO date with JavaScript.

In this article, we’ll look at how to convert a date string to ISO date with JavaScript.

How to convert a date string to ISO date with JavaScript?

To convert a date string to ISO date with JavaScript, we can use the date’s toISOString method.

For instance, we write:

const dateString = '12/01/2022'
const s = new Date(dateString).toISOString()
console.log(s)

to create a Date instance from dateString.

Then we call toISOString to return an ISO 8601 date string.

Therefore, s is "2022-12-01T08:00:00.000Z".

Conclusion

To convert a date string to ISO date with JavaScript, we can use the date’s toISOString method.

Categories
JavaScript Answers

How to call an async function within a Promise .then with JavaScript?

Sometimes, we want to call an async function within a Promise .then with JavaScript.

In this article, we’ll look at how to call an async function within a Promise .then with JavaScript.

How to call an async function within a Promise .then with JavaScript?

To call an async function within a Promise .then with JavaScript, we can return the result of the async function call in the then callback.

For instance, we write:

const f = async () => 'foo'

Promise.resolve(1)
  .then((res) => {
    console.log(res)
    return f()
  })
  .then((res) => {
    console.log(res)
  })

to create the f async function.

Then we call f in the then callback to trigger the call of the 2nd then callback which logs the resolved value of the promise returned by f.

Therefore, we see

1
"foo"

logged.

Conclusion

To call an async function within a Promise .then with JavaScript, we can return the result of the async function call in the then callback.

Categories
JavaScript Answers

How to use JavaScript to create a client side email?

Sometimes, we want to use JavaScript to create a client side email.

In this article, we’ll look at how to use JavaScript to create a client side email.

How to use JavaScript to create a client side email?

To use JavaScript to create a client side email, we can create a mailto URL.

For instance, we write:

const addresses = "foo@example.com";
const body = "hello"
const subject = "subject"
const href = `mailto:${addresses}?subject=${subject}&body=${body}`
const wndMail = window.open(href, "_blank", "scrollbars=yes,resizable=yes,width=10,height=10");

to create the href mailto URL.

We combine the addresses to send to, email subject and email body into one string.

And then we call window.open with href to open the URL, which opens the default mail client with the compose email window.

Conclusion

To use JavaScript to create a client side email, we can create a mailto URL.

Categories
JavaScript Answers

How to compare two variables containing strings in JavaScript?

Sometimes, we want to compare two variables containing strings in JavaScript.

In this article, we’ll look at how to compare two variables containing strings in JavaScript.

How to compare two variables containing strings in JavaScript?

To compare two variables containing strings in JavaScript, we can use the === operator.

For instance, we write:

const a = 'foo'
const b = 'foo'
console.log(a === b)

to compare strings a and b with === to check if they’re equal.

Therefore, the console log logs true since they’re the same.

Conclusion

To compare two variables containing strings in JavaScript, we can use the === operator.