我擅长 PHP,但不懂 Javascript,所以我不知道如何继续。我正在尝试用第 3 行中的“+”替换空格。有人知道为什么它不起作用吗?谢谢!
var tn_dv_suggestions = new Array();
for(var tn_counter=0; tn_counter < tn_top_performers.length; tn_counter++)
tn_top_performers[tn_counter]=tn_top_performers[tn_counter].replace(" ","+");
tn_dv_suggestions.push("<a style='font-family: Verdana, Arial; font-size: 14px;' target='_blank' href='http://www.<?=$siterow['Domain']?>/Buy-"+escape(tn_top_performers[tn_counter]) +"-<?=urlencode($siterow['CitySearchName'])?>-Tickets' >"+tn_top_performers[tn_counter] +"</a><br />");
document.getElementById('tn_dv_suggestions089hZ').innerHTML=tn_dv_suggestions.join('');
最佳答案
这是使用 array.map 的解决方案:
var replaceInArray = function(str){
return str.replace(/\s+/g, "+")
}
var arr = ["Summer is Great", "Winter is terrible"]
arr.map(replaceInArray);
// returns => ["Summer+is+Great", "Winter+is+terrible"]
您的问题是您只替换了“”的第一个实例。要解决此问题,请使用全局标志,将 g
与正则表达式结合使用。
关于Javascript 替换数组中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462404/