const carString = `批发价:17.9
【车辆款型】奔驰CLA200
【上牌日期】19-1
【出厂日期】2017.2
【新车指导价
【排 量】1.6
【排放标准】5
【真实公里】82000
【手动自动】自动
【车辆配置】17年动感版
【详细车况】和记录一样,水箱框架更换,大灯
【过户次数】0
【联系电话】15165923323
【车辆所在地】德州`
const priceResult = carString.match(/价格*(:|:)(\d+.*\d*)(\n|\r)/)
const priceOne = (priceResult && priceResult[2]) || ''
let priceValue = priceOne
if (parseFloat(priceOne) >= 10000) {
priceValue = (priceOne / 10000)
if (/\./.test(priceOne)) {
priceValue = priceOne.toFixed(2)
}
}
const seriesResult = carString.match(/(款型|型号|名称)】(.*?)(\n|\r)/)
const seriesValue = (seriesResult && seriesResult[2]) || ''
const configureResult = carString.match(/配置】(.*?)(\n|\r)/)
const configureValue = (configureResult && configureResult[1]) || ''
const plResult = carString.match(/量】(\d+.*\d*)(\n|\r)/)
const plValue = (plResult && plResult[1]) || ''
const ghResult = carString.match(/过户次数】(.*?)(\n|\r)/)
let ghOne = (ghResult && ghResult[1]) || ''
let ghValue = '未知'
if (/\d+/.test(ghOne)) {
ghValue = ghOne.match(/\d+/)[0]
} else {
if (/(一手|车主名下)/.test(ghOne)) {
ghValue = 0
}
}
const kmResult = carString.match(/公里】(\d+.*\d*)(\n|\r)/)
const kmOne = (kmResult && kmResult[1]) || ''
let kmValue = kmOne
if (parseFloat(kmOne) >= 10000) {
kmValue = (kmOne / 10000)
if (/\./.test(kmValue)) {
kmValue = kmValue.toFixed(1)
}
}
const dateResult = carString.match(/上牌日期】(.*?)(\n|\r)/)
let dateOne = (dateResult && dateResult[1]) || ''
let dateValue = dateOne || '未上牌'
if (/\./.test(dateOne)) {
dateValue = formatYearMonth(dateOne.split('.').slice(0, 2))
} else if (/\-/.test(dateOne)) {
dateValue = formatYearMonth(dateOne.split('-').slice(0, 2))
}
function formatYearMonth(arr) {
let dateValue = ''
if (/\d+/.test(arr[0])) {
dateValue = arr[0].match(/\d+/)[0] + '年'
}
if (/\d+/.test(arr[1])) {
dateValue += arr[1].match(/\d+/)[0] + '月'
}
return dateValue
}
const bsxResult = carString.match(/手动自动】(.*?)(\n|\r)/)
const bsxValue = (bsxResult && bsxResult[1]) || ''
const ckResult = carString.match(/车况】(.*?)(\n|\r)/)
const ckValue = (ckResult && ckResult[1]) || ''
const telResult = carString.match(/电话】(.*?)(\n|\r)/)
const telValue = (telResult && telResult[1]) || ''
const proResult = carString.match(/地】(.*?)((\n|\r)|$)/)
const proValue = (proResult && proResult[1]) || ''
const data = {
carValue: seriesValue,
carType: seriesValue,
carAllName: seriesValue,
desc: true,
bcsmtxt: configureValue,
date: dateValue,
km: kmValue,
ghnum: ghValue,
checked: true,
pl: plValue + 'T',
bsx: bsxValue,
price: priceValue,
currentZk: '其他',
damageValue: '',
currentCk: '其他',
ztckValue: ckValue,
telValue,
proValue,
}
console.log(data)