admin管理员组文章数量:1313782
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));
The code above is supposed to replace all occurrences of "special" characters ([newline], etc) with their HTML equivalents. But it's not working.
Why?
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));
The code above is supposed to replace all occurrences of "special" characters ([newline], etc) with their HTML equivalents. But it's not working.
Why?
Share Improve this question asked Jul 13, 2011 at 18:54 RyanRyan 6181 gold badge7 silver badges16 bronze badges 1-
Besides the below, you will also need to get rid of
this.
fromthis.formatChart[k]
.this
refers to your formatString function – bcoughlan Commented Jul 13, 2011 at 19:05
8 Answers
Reset to default 4Be carefull, replace in javascript works with regex. This is not what you are trying to do. An usual way to do is use bined join and split functions.
Plus, you are testing if the replaced string exists in a first place (formatChart[k]) but you want to test if the replacee (k) is in that string.
here is a sample code :
function formatString(str) {
for (var k in formatChart) {
str = str.split(k).join(formatChart[k]);
}
return str;
}
You are searching the string for the resultant values, not the keys. Try this instead:
var formatString = function(str)
{
for (var k in formatChart)
{
while (str.indexOf(k) != -1)
str = str.replace(k, formatChart[k]);
}
return str;
};
string.indexOf(formatChart[k]) != -1
is wrong. When iterating over an Object (which you actually shouldn't do) the k
value is the Key. You want string.indexOf(k) != -1
.
Instead of this:
while (string.indexOf(formatChart[k]) != -1)
Try this:
while (string.indexOf(k) != -1)
There is an small mistake in your function. replace
while (string.indexOf(formatChart[k]) != -1)
by
while (string.indexOf(k) != -1)
and see the results
Here's a slightly different regex version. This escapes the regex chars in the things to replace so we can use the global replace of the regex replace function. You need double backslashes in front of the brackets so that you're left with on backslash when passed as a regex.
var formatChart = {
'\\[newline\\]' : '<br />',
'\\[tab\\]' : ' ',
'\\[space\\]' : ' '
};
var str = "Hello[newline]World[tab]Tab[space]Hello[newline]Done";
function formatString(str) {
for (var i in formatChart) {
str = str.replace(new RegExp(i, "gi"), formatChart[i]);
}
return(str);
}
You can see it in action here: http://jsfiddle/jfriend00/pj2Kr/
Here's a fiddle using regex: http://jsfiddle/msTuC/
Incorrect: while (string.indexOf(formatChart[k]) != -1)
Correct: while (string.indexOf(k) != -1)
本文标签: JavaScript StringReplace() not workingStack Overflow
版权声明:本文标题:JavaScript String.Replace() not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741924174a2405185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论