const parseTimeString = (timeString) => {
if (/^\d+$/.test(timeString)) {
return `${timeString}个月`; // 纯数字自动加上月单位
}
const monthMatch = timeString.match(/(\d+)个月/);
const dayMatch = timeString.match(/(\d+)天/);
const months = monthMatch ? `${monthMatch[1]}个月` : '';
const days = dayMatch ? `${dayMatch[1]}天` : '';
// 如果只有天数或者只有月数,直接返回
if (months && !days) return months;
if (days && !months) return days;
// 如果有月数也有天数,返回组合的字符串
return `${months}${days}`;
};
时间字符串可能是“1天”
时间字符串可能是“39个月”
时间字符串可能是“39个月1天”
时间字符串可能是“39”
这个方法可以根据4种字符串都把数字截取出来根据天跟日4种其他再return返回回去想要的格式