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.
For this simple scenario, i would also recommend to use the methods that comes build-in in javascript.
You could try this :
Greetings
@scripto's made a bit more concise and without
prototype
:Here's how it stacks up: http://jsperf.com/replace-vs-split-join-vs-replaceall/68
You need to escape the
.
because it has the meaning of "an arbitrary character" in a regular expression.Here's another implementation of replaceAll. Hope it helps someone.
Then you can use it:
var myText = "My Name is George";
var newText = myText.replaceAll("George", "Michael");
This is more concise/readable and should perform better than the one posted by Fagner Brack (toLowerCase not performed in loop):
Usage: