There is a replace function to change some character in a string. You can replace the defined instances or some constructions using regular expressions. To change all instances in a string it is necessary to set the defined flag g in a regular expression.
Space character in the regular expressions is \s. If there is more than one space in one place in a string the construction of regex will be \s+. Like it was said earlier to change all instances of space there should be flag g in the regular expression.
var str = ‘This is a simple string’;
var charYouWant = ‘’;
String.prototype.replaceAll = function(needle, replaceTo)
var str = this.replace(new RegExp(needle, 'g'), replaceTo);
This function returns a string. But the original function will not be changed.
To replace all spaces in a string using replaceAll function you should do the next:
var newString = str.replaceAll(‘ ’, charYouWant);