可能重复:
Fastest method to replace all instances of a character in a string
如何替换在字符串中找到的所有匹配项?
如果要替换字符串中的所有换行符(\n) ..
这将只替换第一个出现的换行符
str.replace(/\\n/, '<br />');
我想不出怎么做这个把戏?
发布于 2011-05-20 05:14:43
使用全局标志。
str.replace(/\n/g, '<br />');
发布于 2011-05-20 05:20:18
Brighams answer使用literal regexp
。
使用Regex对象的解决方案。
var regex = new RegExp('\n', 'g');
text = text.replace(regex, '<br />');
在这里试试:JSFiddle Working Example
发布于 2012-02-27 09:30:39
正如here所解释的,您可以使用:
function replaceall(str,replace,with_this)
{
var str_hasil ="";
var temp;
for(var i=0;i<str.length;i++) // not need to be equal. it causes the last change: undefined..
{
if (str[i] == replace)
{
temp = with_this;
}
else
{
temp = str[i];
}
str_hasil += temp;
}
return str_hasil;
}
..。然后,您可以使用以下命令调用它:
var str = "50.000.000";
alert(replaceall(str,'.',''));
该函数将警告"50000000“
https://stackoverflow.com/questions/6064956
复制相似问题