How to remove white space

Asked

Viewed 451 times

2

I have the following code

var ola = "242 052";

How do I remove space from the variable? For example, so:

var ola = "242052";

Thank you

2 answers

5


To a space:

var ola = "242 052";

ola = ola.replace(" ","")

console.log(ola);

More than one space:

var ola = "242  052";
   
ola = ola.replace( /\s/g, '' )

console.log(ola);

The global match g - finds all occurrences of the expression in the text, instead of stopping at the first occurrence.

var ola = "242  052 098 09            8         7";
   
ola = ola.replace( / /g, '' )

console.log(ola);

  • Thanks, it worked!

0

var ola = '123 456 789';

ola = ola.split(' ').join('');

console.log(ola);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.