我想改變在字符串中的字符與許多值如何替換字符串中的字符有許多字符
我有串這樣的:
date_format = "%m/%d/%Y";
,我想,以取代日益%焦炭這之後,所以日期變量應該是這樣的:
date_format="mm/dd/YY";
這裏是我試過到目前爲止,但我不能得到它的工作,所以我在這裏需要一些幫助:
function replaceon(str, index, chr) {
if (index > str.length - 1) return str;
return str.substr(0, index) + chr + str.substr(index + 1);
}
function locations(substring, string) {
var a = [],
i = -1;
while ((i = string.indexOf(substring, i + 1)) >= 0) a.push(i);
return a;
}
function corrent_format(date_format) {
var my_locations = locations('%', date_format);
console.log(my_locations.length);
for (var i = 0; i < my_locations.length; i++) {
replaceon(date_format, my_locations[i], date_format[my_locations[i] + 1]);
}
return date_format;
}
console.log(corrent_format(date_format));
它的工作完美:)謝謝 –