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
  • 204,283
  • 31
  • 260
  • 272
Charky
  • 66
  • 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
  • 37,143
  • 5
  • 39
  • 60
1

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

Ezra
  • 1,118
  • 9
  • 13