-1

Instead it changes everything in the string to dashes. Can someone explain why it is doing that? Here is the expression:

var replaceDots = function(str) {
    return str.replace(/./g, '-');
}

I have been trying to figure this out for awhile now. I know it will work without being placed inside of the var replaceDots. It needs to be inside the variable to complete the question.

marcadian
  • 2,608
  • 13
  • 20
Artzelij
  • 1
  • 1
  • With RegEx, the period `.` is a wildcard that represents any character, which is why it changes everything to dashes. Try this: `str.replace(/\./g, '-')` since if you want to represent the period itself and not a wildcard, you need to escape it with the backslash `\`. – victor Jul 19 '17 at 23:41

1 Answers1

4

. has a special meaning in regexes. It means "any character" or "any character except newline" depending on the flavour (it excludes line terminators in JS). You want /\./g (i.e. escape the .)

mpen
  • 272,448
  • 266
  • 850
  • 1,236