Skip to content

Instantly share code, notes, and snippets.

@lichspace
Created August 3, 2018 10:05
Show Gist options
  • Select an option

  • Save lichspace/0f2d202bf1fcb9ecf16c544e15b0ef6c to your computer and use it in GitHub Desktop.

Select an option

Save lichspace/0f2d202bf1fcb9ecf16c544e15b0ef6c to your computer and use it in GitHub Desktop.
JS嵌套匹配
parseHmtlToJson(html,cnt_attr) {
//timeline 处理
let timeLineJson = []
html = html.replace(/<!--TIMELINE-->(.*?)<!--\/TIMELINE-->/,(match)=>{
//console.log(match)
timeLineJson = match.match(/<!--(TIME|EVENT)-->(.*?)<!--\/(TIME|EVENT)-->/g).map(v=>{
let r = v.match(/<!--(TIME|EVENT)-->(?:<P>)*(.*?)(?:<\/P>)*<!--\/(TIME|EVENT)-->/)
return r?{
type:r[1],
value:r[2]
}:{}
})
return '<!--TIMELINE-->'
})
//正文处理
let r = html.split(/<P>(.*?)<\/P>/)
if(!r) return []
let json = r.filter(v=>!!v).map(v=>{
let value = v
let node = {
type:1,
value
}
let type = value.match(/<!--(.*?)-->/)
if(type){
type=type[1]
if(type.startsWith('IMG')){
node={
type:2,
value:cnt_attr[type]['img']['imgurlgif']?cnt_attr[type]['img']['imgurlgif']['imgurl']:cnt_attr[type]['img']['imgurl1000']['imgurl']
}
}
if(type.startsWith('VIDEO')){
node={
type:3,
value:{
vid:cnt_attr[type]['vid'],
img:cnt_attr[type]['img']['imgurl1000']['imgurl'],
desc:cnt_attr[type]['desc']
}
}
}
if(type.startsWith('TIMELINE')){
node={
type:'TIMELINE',
value:timeLineJson
}
}
//console.log(type)
}
return node
})
return json
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment