Navigate back to the homepage

how to remove space between strings in javascript?

Infinitbility
JavaScript
July 23rd, 2022 · 1 min read
how to remove space between strings in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove space between strings in javascript, just use the replace() method with the /\s/g regex then it will return a new string without spaces.

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If the pattern is a string, only the first occurrence will be replaced.

Let’s take an example to remove space between strings in javascript using the replace() method.

1const regex = /\s/g;
2
3var str = "site logo.png";
4
5str.replace(regex, '');
6// 👇️ Output
7// "sitelogo.png"

Today, I’m going to show you How do I remove space between strings in javascript, as above mentioned here, I’m going to use the replace() method with the /\s/g regex or split() method with the join().

Let’s start today’s tutorial how do you remove space between strings in javascript?

In this example, we will do

  1. example of remove space between string using replace with regex
  2. example of removing space between strings using split() with join()

Let’s write code…

example of remove space between string using replace with regex

Using replace() and /\s/g we are finding spaces in string and replaceing width empty string.

1const regex = /\s/g;
2
3var str = "site logo.png";
4
5str = str.replace(regex, '');
6// 👇️ Output
7// "sitelogo.png"

example of remove space between string using split() with join()

  1. Using the split() method, we are making an array of elements based on the number of spaces in the string.
  2. Using the join() method, we are joined the array of elements to a single string.
1var str = "site logo.png";
2
3str = str.split(' ').join('');
4// 👇️ Output
5// "sitelogo.png"

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to add character in string in javascript?

how to add character in string in javascript?

An example of adding character in string in javascript with step by step guide.

July 22nd, 2022 · 1 min read
how to delete flow in node red?

how to delete flow in node red?

Example of removing flows from the node-red with the step-by-step guide.

July 21st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]