var formatChart = {
'[newline]' : '<br />',
'[tab]' : ' ',
'[space]' : ' '
};
// Formats a string according to the formatting chart
var formatString = function(string)
{
for (var k in formatChart)
{
while (string.indexOf(formatChart[k]) != -1)
string = string.replace(k, this.formatChart[k]);
}
return string;
};
var str = "Hello[newline]World[tab]Tab[space]Hello[newline]Done";
alert(formatString(str));
上面的代码应该用它们的HTML替换替换所有出现的“特殊”字符([newline]等)。但它不起作用。JavaScript String.Replace()不起作用
为什么?
除了下面,你也将需要从'this.formatChart [K]摆脱''this.'。 'this'指向你的formatString函数 – bcoughlan