我想替换JavaScript字符串中出现的所有点(.)

例如,我有:

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

我想要得到:这是一个字符串。

到目前为止,我尝试了:

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

但这最终将所有字符串替换为空格。


当前回答

String.prototype.replaceAll = function(character,replaceChar){
    var word = this.valueOf();

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

    return word;
}

其他回答

我加了双反斜杠的点,使它工作。欢呼。

var st = "okay.this.is.a.string";
var Re = new RegExp("\\.","g");
st = st.replace(Re," ");
alert(st);
String.prototype.replaceAll = function (needle, replacement) {
    return this.replace(new RegExp(needle, 'g'), replacement);
};

您可以使用RegExp javascript对象替换任何字符串/字符的所有出现。

这是代码,

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

var patt = new RegExp("\\.");

while(patt.test(mystring)){

  mystring  = mystring .replace(".","");

}

对于这个简单的场景,我也建议使用javascript内置的方法。

你可以试试这个:

"okay.this.is.a.string".split(".").join("")

问候

str.replace(new RegExp(".","gm")," ")