Categories
JavaScript Answers

How to extract parameter value from URL using regular expressions and JavaScript?

Sometimes, we want to extract parameter value from URL using regular expressions and JavaScript.

In this article, we’ll look at how to extract parameter value from URL using regular expressions and JavaScript.

How to extract parameter value from URL using regular expressions and JavaScript?

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

For instance, we write

const regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
const url = "http://www.youtube.com/watch?v=Ahg6qcgoay4";
const [, id] = url.match(regex);

to call url.match with the regex that has a capture group ([\w-]{11}) to get the value of the v query parameter.

Then we call url.match with regex to return an array with the video’s id as the 2nd value of the array.

Conclusion

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

Categories
JavaScript Answers

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

Sometimes, we want to compose multipart/form-data with a different Content-Type on each part with JavaScript.

In this article, we’ll look at how to compose multipart/form-data with a different Content-Type on each part with JavaScript.

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

For instance, we write

const formData = new FormData();

formData.append(
  "items",
  new Blob(
    [
      JSON.stringify({
        name: "Book",
        quantity: "12",
      }),
    ],
    {
      type: "application/json",
    }
  )
);

to call formData.append with the key and a blob.

In the blob, we have the JSON content as a string in an array.

And we set the MIME type of the content by setting type to "application/json".

Conclusion

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

Categories
JavaScript Answers

How to hide the address bar on iPhone with JavaScript?

Sometimes, we want to hide the address bar on iPhone with JavaScript.

In this article, we’ll look at how to hide the address bar on iPhone with JavaScript.

How to hide the address bar on iPhone with JavaScript?

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.

For instance, we write

window.scrollTo(0, 1);

to call scrollTo to scroll the browser tab or window 1 pixel down.

Conclusion

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.

Categories
JavaScript Answers

How to use Jest to spy on a method call with JavaScript?

Sometimes, we want to use Jest to spy on a method call with JavaScript.

In this article, we’ll look at how to use Jest to spy on a method call with JavaScript.

How to use Jest to spy on a method call with JavaScript?

To use Jest to spy on a method call with JavaScript, we call the spyOn method.

For instance, we write

const spy = jest.spyOn(Component.prototype, "methodName");
const wrapper = mount(<Component {...props} />);
wrapper.instance().methodName();
expect(spy).toHaveBeenCalled();

in our test to call spyOn to add a spy to the Component‘s methodName instance method.

The we call the methodName method on the component.

And we check if it’s called with with toHaveBeenCalled.

Conclusion

To use Jest to spy on a method call with JavaScript, we call the spyOn method.

Categories
JavaScript Answers

How to split and modify a string in Node.js and JavaScript?

Sometimes, we want to split and modify a string in Node.js and JavaScript.

In this article, we’ll look at how to split and modify a string in Node.js and JavaScript.

How to split and modify a string in Node.js and JavaScript?

To split and modify a string in Node.js and JavaScript, we use the split and map methods.

For instance, we write

const str = "123, 124, 234,252";
const arr = str.split(",").map((val) => Number(val) + 1);
console.log(arr);

to call str.split to split the string into a string array by the comma.

Then we call map with a callback to map each value to the value we want.

Conclusion

To split and modify a string in Node.js and JavaScript, we use the split and map methods.