0

var mystring = "this.is.a.test";
mystring = mystring.replace(/./g, "X");
console.log(mystring);

I expect the output of thisXisXaXtest but my log show XXXXXXXXXXXXXX

j08691
  • 197,815
  • 30
  • 248
  • 265
Charky
  • 86
  • 6

2 Answers2

5

. is special character in regex ( which means match anything except new line ) you need to escape it

var mystring = "this.is.a.test";
mystring = mystring.replace(/\./g, "X");
console.log(mystring);

DOT

Code Maniac
  • 35,187
  • 4
  • 31
  • 54
1

. matches every character other than newlines. To use it how you are intending, it needs to be escaped: \..

Ezra
  • 923
  • 6
  • 12