Remove space between line using javascript

I have a line. I just want to remove all spaces between all characters. Please answer "PB 10 CV 2662" to "PB10CV2662"

+4
source share
7 answers

This should do the trick:

var str = "PB 10 CV 2662"; str = str.replace(/ +/g, ""); 
+23
source

Try the following:

 var s = "PB 10 CV 2662"; s.replace(/\s+/g, ''); 

OR

 s.replace(/\s/g, ''); 
+3
source
 var str = "PB 10 CV 2662"; str = str.split(" ") //[ 'PB', '10', 'CV', '2662' ] str = str.join("") // 'PB10CV2662' OR in one line: str = str.split(" ").join("") 
+2
source

The easiest way is to use the replace() method on strings:

 var stringVal = "PB 10 CV 2662"; var newStringVal = stringVal.replace(/ /g, ""); 

This will take the current string value and create a new one where all spaces will be replaced with empty strings.

0
source
 var str = "PB 10 CV 2662"; var cleaned = str.replace(/\s+/g, ""); 
0
source

Try:

 var sample_str = "PB 10 CV 2662" var new_str = sample_str.split(" ").join("") 

Or you can use .replace with a global flag:

  var sample_str = "PB 10 CV 2662" var new_str = sample_str.replace(" ","","g") 

Both will cause new_str to be equal to "PB10CV2662". I hope this is useful to you.

0
source
 value = value.replace(/(\r\n\s|\n|\r|\s)/gm, ''); 
-one
source

Source: https://habr.com/ru/post/1496285/


All Articles