Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to replace all the occurances of a period (.) 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.

Any idea how to resolve this?

share|improve this question
4  
aefxx's answer is correct, but just as an FYI is that the period character in a regex means match everything, thus everything being a space. Escaping it with the backslash means match on periods. – swilliams Jul 27 '10 at 16:52
Thanks for the tip. I have has some AHA moments (when building the app) with Regex. I really hate it _, do you have some cool, good tutorial? – Omar Abid Jul 31 '10 at 1:32
rubular.com is what you're looking for – LanguagesNamedAfterCofee Jul 17 '12 at 15:41

6 Answers

up vote 206 down vote accepted

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

mystring.replace(/\./g,' ')
share|improve this answer
5  
just to clarify, the \ escapes special characters in regular expressions, like the . in this case – realgt Sep 27 '11 at 20:30
looks like sed.. somehow.. :) – Paschalis Jul 9 '12 at 19:23

One more solution which is easy to understand :)

var newstring = mystring.split('.').join(' ');
share|improve this answer
4  
I like this for some reason. – HaggleLad Mar 23 '12 at 9:09
10  
@HaggleLad because you don't need to mess with regex – ton.yeung Apr 4 '12 at 3:30
4  
Isn't this much slower than regexing? – Jasper Kennis Jun 6 '12 at 14:10
42  
4  
@BetoFrega Nothing like some empirical data to make your case :). Thanks for providing the link! – testing123 Sep 28 '12 at 4:41
show 4 more comments
/**
 * ReplaceAll by Fagner Brack (MIT Licensed)
 * Replaces all occurrences of a substring in a string
 */
String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
    var _token;
    var str = this + "";
    var i = -1;

    if ( typeof token === "string" ) {

        if ( ignoreCase ) {

            _token = token.toLowerCase();

            while( (
                i = str.toLowerCase().indexOf(
                    token, i >= 0 ? i + newToken.length : 0
                ) ) !== -1
            ) {
                str = str.substring( 0, i ) +
                    newToken +
                    str.substring( i + token.length );
            }

        } else {
            return this.split( token ).join( newToken );
        }

    }
return str;
};

alert('okay.this.is.a.string'.replaceAll('.', ' '));

Faster than using regex...

share|improve this answer
1  
thanks a lot :) – peplamb May 2 '12 at 23:41
1  
Very useful. FYI: There are rogue characters after the semi-colon in the alert statement. – Patrick Nov 27 '12 at 11:24
What you mean for "rogue character"? – Fagner Brack Feb 6 at 23:09
1  
He means entity & #8203 ; twice, which is Unicode Character 'ZERO WIDTH SPACE' (U+200B). More information on fileformat.info/info/unicode/char/200b/index.htm – Cœur Feb 7 at 14:07
Oh got it, thanks! – Fagner Brack Feb 7 at 20:33
str.replace(new RegExp(".","gm")," ")
share|improve this answer
1  
Worked great for replace function =) – afreeland Jul 26 '12 at 18:09

I add double backslash to the dot to make it work. Cheer.

var st = "okay.this.is.a.string";
var Re = new RegExp("\\.","g");
st = st.replace(Re," ");
alert(st);
share|improve this answer
String.prototype.replaceAll = function(char,replaceChar){
    var word = this.valueOf();

    while(word.indexOf(char) != -1)
        word = word.replace(char,replaceChar);

    return word;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.