2011-04-13 31 views

回答

9

您需要进行全局替换。不幸的是,你不能这样做跨浏览器使用字符串参数:您的需要,而不是一个正则表达式:

ss.replace(/,/g, '\n\t'); 

g修改器使得搜索全球。

+0

谢谢你们....它的工作...... – Warrior 2011-04-13 12:48:25

2

你需要在这里使用正则表达式。请尝试以下方法

ss.replace(/,/g,”\n\t”) 

g表示全局替换它。

+0

谢谢你们....它工作...... – Warrior 2011-04-13 12:48:00

0

这里是replaceAll的另一个实现。希望它能帮助别人。

String.prototype.replaceAll = function (stringToFind, stringToReplace) { 
     if (stringToFind === stringToReplace) return this; 
     var temp = this; 
     var index = temp.indexOf(stringToFind); 
     while (index != -1) { 
      temp = temp.replace(stringToFind, stringToReplace); 
      index = temp.indexOf(stringToFind); 
     } 
     return temp; 
    }; 

然后你可以使用它:

会将myText VAR = “我的名字是乔治”;
var newText = myText.replaceAll(“George”,“Michael”);