i’ve rummaged around and found some useful posts for doing find / search / replace in jquery using a key value pair array / object,
but i can’t get it to work,
here’s the site’s test URL:
http://www.larryadowns.com.php5-1.dfw1-2.websitetestlink.com/
as you can see, it’s a blog feed with post info. i’m trying to target the date months, and do a search and replace for each to put in the spanish months.
here’s the javascript, it’s all wrapped in a 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;
});
but alas, neither the .replace or the .replace with the RegEx are really replacing anything.
where did i go wrong?
ty again stack.
Not quite sure why, but it seems your code was returning the
moddedTextto it’s original as it ran through subsequent months. Because of this, it was only replacing December correctly.I’ve used a slightly different method but it should produce what you’re looking for.
Check this jsFiddle for full code and demo.