Removing white space [duplicate]

3

I have the following code

var ola = "242 052";

How do I remove space from the variable? To make it look like this:

var ola = "242052";

Thank you

    
asked by anonymous 12.07.2018 / 18:10

2 answers

5

For 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 - searches for all occurrences of the expression in the text, rather than stopping at the first occurrence.

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

console.log(ola);
    
12.07.2018 / 18:20
0

var ola = '123 456 789';

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

console.log(ola);
    
12.07.2018 / 19:23