Pedro Relvas
Pedro Relvas

Reputation: 679

Why .replace method doesn't work this way?

I want to clear all the whitespaces in the string arrayIsNowJoined, and everywhere I look up target the solution I have...but it just doesn't work. I got the following code:

const array1 = ["1, 3, 4, 7, 16", "1, 2, 4, 16"];
let arrayIsNowJoined = array1.join();

arrayIsNowJoined.replace(/\s/g, "");
console.log(arrayIsNowJoined);

What I'm doing wrong?

Upvotes: 2

Views: 35

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386736

You need an assignment of the result, because strings are immutable.

const array1 = ["1, 3, 4, 7, 16", "1, 2, 4, 16"];
let arrayIsNowJoined = array1.join();

arrayIsNowJoined = arrayIsNowJoined.replace(/\s/g, "");
console.log(arrayIsNowJoined);

Upvotes: 2

kamilyrb
kamilyrb

Reputation: 2627

You must assign variable after replace

arrayIsNowJoined = arrayIsNowJoined.replace(/\s/g, "");

Upvotes: 3

Related Questions