How to replace all dots in a string using JavaScript

Omar Abid picture Omar Abid · Mar 6, 2010 · Viewed 550.7k times · Source

I want to replace all the occurrences of a dot(.) in a JavaScript string

For example, I have:

var mystring = 'okay.this.is.a.string';

I want to get: okay this is a string.

So far I tried:

mystring.replace(/./g,' ')

but this ends up with all the string replaced to spaces.

Answer

aefxx picture aefxx · Mar 6, 2010

You need to escape the . because it has the meaning of "an arbitrary character" in a regular expression.

mystring = mystring.replace(/\./g,' ')