【发布时间】:2012-07-09 01:02:48
【问题描述】:
我翻遍了,发现了一些有用的帖子,用于在 jquery 中使用键值对数组/对象进行查找/搜索/替换,
但我不能让它工作,
这是网站的测试网址:
http://www.larryadowns.com.php5-1.dfw1-2.websitetestlink.com/
如您所见,这是一个包含帖子信息的博客提要。我正在尝试定位日期月份,并为每个月份进行搜索和替换以放入西班牙月份。
这里是 javascript,它都被包裹在一个 jQuery(document).ready()...
var monthMap = {
"January" : "Enero",
"February" : "Febrero",
"March" : "Marzo",
"April" : "Abril",
"May" : "Mayo",
"June" : "Junio",
"July" : "Julio",
"August" : "Agosto",
"September" : "Septiembre",
"October" : "Octubre",
"November" : "Noviembre",
"December" : "Diciembre"
};
// sift thru the post-info, replacing only the month with the spanish one.
$(".post-info .date").text(function(index, originalText) {
var moddedText = '';
for ( var month in monthMap ) {
if (!monthMap.hasOwnProperty(month)) {
continue;
}
moddedText = originalText.replace(month, monthMap[month]);
// moddedText = originalText.replace( new RegExp(month, "g") , monthMap[month] );
console.log("month : " + month);
console.log("monthMap[month] : " + monthMap[month]);
}
console.log('-------------------');
console.log('index : ' + index);
console.log("monthMap : " + monthMap);
console.log("originalText : " + originalText);
console.log("moddedText : " + moddedText);
return moddedText;
});
但是,.replace 或带有 RegEx 的 .replace 都没有真正替换任何东西。 我哪里做错了? ty 再次堆叠。
【问题讨论】:
标签: jquery arrays search object replace