0

I'm trying to figure out how to replace all the whitespaces with a character given. My code is duplicating the character when the whitespace is replaced.

This is the exercise, and my code so far:

This function receives a string. It also takes an optional character, which should default to an underscore ('_') when not given.

It should return the same string, but with all groups of whitespace (spaces, tabs and line breaks) replaced by a single instance of the character in the second argument.

function replaceWhitespaceWithCharacter (str, character) {
  return str.replace(/\s/g, character);
}

Also, I don't know how to default the underscore. Could anyone give me a hand, please? Thank you in advance.

Andreas
  • 21,535
  • 7
  • 47
  • 56
VisualXZ
  • 213
  • 3
  • 17
  • https://www.regular-expressions.info/repeat.html – bobble bubble Mar 24 '18 at 12:52
  • *"Also, I don't know how to default the underscore"* Best to ask **one** question/question, and also be sure to [search thoroughly](/help/searching) as the topic of how to default parameter values is very well-covered on SO (and elsewhere). – T.J. Crowder Mar 24 '18 at 12:54

7 Answers7

4

/\s/g matches one character of whitespace (repeatedly). To match one or more characters of whitespace in a row, use + after \s: /\s+/g.

function replaceWhitespaceWithCharacter (str, character) {
  return str.replace(/\s+/g, character);
}

Also, I don't know how to default the underscore

Before ES2015, you'd do something like this:

function replaceWhitespaceWithCharacter (str, character) {
  character = character || "_";
  return str.replace(/\s+/g, character);
}

...because character will be undefined if not given, which is falsy. Or if you want to allow for "" (which is also falsy), then:

function replaceWhitespaceWithCharacter (str, character) {
  character = typeof character === "undefined" ? "_" : character;
  return str.replace(/\s+/g, character);
}

As of ES2015, you can use a default parameter value:

function replaceWhitespaceWithCharacter (str, character = "_") {
  return str.replace(/\s+/g, character);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

To include multiple white spaces you should add + like:/\s+/g

So your code should be:

function replaceWhitespaceWithCharacter (str, character) {
  return str.replace(/\s+/g, character);
}
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

In regular expression one or more matches. So /\s+/ means white spaces least one max any. Then I am checking whether character is defined or not with ternary operator. If it is not then i am assigning _.

function replaceWhitespaceWithCharacter (str, character) {
  return str.replace(/\s+/g, character?character: '_');
}
Mr.Pandya
  • 1,899
  • 1
  • 13
  • 24
1

Defaulting to '_', insert :

var chr = (typeof chr !== 'undefined') ? chr : '_';

Replacing groups of white spaces with one or more elements in the group (but not zero) :

  return str.replace(/\s+/g, chr);

So we end up with :

function replaceWhitespaceWithCharacter (str, chr) {
  var chr = (typeof chr !== 'undefined') ? chr : '_';
  return str.replace(/\s+/g, chr);
}
Siltaar
  • 173
  • 1
  • 7
0

You can also extend the string object with the method:

String.prototype.replaceWhitespaceWithCharacter = function replaceWhitespaceWithCharacter(theCharacter) 
{
    if ((typeof theCharacter !== 'string') || (theCharacter === ''))
    {
        theCharacter = '_';
    }
    return this.replace(/\s+/g, theCharacter);
}

My checkings for the character based on typeof for string or if theCharacter is a empty string

Snow
  • 141
  • 1
  • 4
-1
var str = "this is  a\ttest\n2";
var str2 = str.replace(/\s+/g,'_');

console.log(str2);

this is replace all white spaces with a single underscore.

-2

This is my final solution. Thanks for your help.

function replaceWhitespaceWithCharacter (str, character) {

       if(character){
        return str.replace(/\s+/g, character);
       }else{
         return str.replace(/\s+/g, '_');
       }
    }
VisualXZ
  • 213
  • 3
  • 17