0

I tried to cut dot from string using the following function:

function removeSymbols(str){

  console.log(str.length);
  str.replace(/\./g, "");
  return str;
}

var str = " народу.";

But it does not cut

Dev
  • 1,013
  • 7
  • 15
  • 37
  • 2
    `.replace` *returns* a new string. – Felix Kling Jan 22 '17 at 01:19
  • Have you tried putting str.replace(".", ""); – Dawcars Jan 22 '17 at 01:21
  • 2
    @Dawcars: Why would that make a difference? – Felix Kling Jan 22 '17 at 01:22
  • @Dawcars its not a matter of his `.replace()` method being incorrect (it is correct), OP needs to return the new string returned from `.replace()` instead of returning the unchanged string that was originally passed into `removeSymbols()`. – Sam Jan 22 '17 at 01:23
  • 1
    @Dawcars also, not sure if it matters here, but FYI, `.replace(".", "")` would only replace the first dot...it wouldn't work if there were multiple dots. – Matt Browne Jan 22 '17 at 01:28

3 Answers3

3

Change your return statement from

return str;

To

return str.replace(/\./g, "");

VHS
  • 9,534
  • 3
  • 19
  • 43
2

function removeSymbols(str) {
    console.log(str.length);
    str = str.replace(/\./g, "");
    return str;
}
var str = " народу.";
console.log(removeSymbols(str));
tklg
  • 2,572
  • 2
  • 19
  • 24
  • This is technically correct, but I don't think it is necessary to reassign `str` when one could just `return str.replace(/\./g, "")` ... I'll still upvote though since it is the first correct answer. – Sam Jan 22 '17 at 01:31
0

replace doesn't change the original string, it will return a new string that is replaced.

Yichong
  • 707
  • 4
  • 10