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.
you can replace all occurrence of any string/character using RegExp javasscript object.
Here is the code,
Faster than using regex...
EDIT:
Maybe at the time I did this code I did not used jsperf. But in the end such discussion is totally pointless, the performance difference is not worth the legibility of the code in the real world, so my answer is still valid, even if the performance differs from the regex approach.
EDIT2:
I have created a lib that allows you to do this using a fluent interface:
See https://github.com/FagnerMartinsBrack/str-replace.