lemooljiang avatar

js提取Markdown文本中的摘要 / 网络研习社#59

lemooljiang

Published: 17 Jan 2020 › Updated: 17 Jan 2020js提取Markdown文本中的摘要  / 网络研习社#59

js提取Markdown文本中的摘要 / 网络研习社#59

steemjiang.jpg

在上一篇中提取了图片,单独显示。正文的摘要也要提取出来以做展示,如上图所示。

不多说,直接到解决方案:

参考

abstractFn(res){
  if(!res){
    return ''
  }else{
    let str=res.replace(/(\*\*|__)(.*?)(\*\*|__)/g,'')          //全局匹配内粗体
            .replace(/\!\[[\s\S]*?\]\([\s\S]*?\)/g,'')           //全局匹配图片
            .replace(/\([\s\S]*?\)/g,'')                         //全局匹配连接
            .replace(/<\/?.+?\/?>/g,'')                         //全局匹配内html标签
            .replace(/(\*)(.*?)(\*)/g,'')                        //全局匹配内联代码块
            .replace(/`{1,2}[^`](.*?)`{1,2}/g,'')                //全局匹配内联代码块
            .replace(/```([\s\S]*?)```[\s]*/g,'')               //全局匹配代码块
            .replace(/\~\~(.*?)\~\~/g,'')                         //全局匹配删除线
            .replace(/[\s]*[-\*\+]+(.*)/g,'')                    //全局匹配无序列表
            .replace(/[\s]*[0-9]+\.(.*)/g,'')                    //全局匹配有序列表
            .replace(/(#+)(.*)/g,'')                            //全局匹配标题
            .replace(/(>+)(.*)/g,'')                            //全局匹配摘要
            .replace(/\r\n/g,"")                                //全局匹配换行
            .replace(/\n/g,"")                                  //全局匹配换行
            .replace(/\s/g,"")                                  //全局匹配空字符;
    return str.slice(0,180)
  }
}

Leave js提取Markdown文本中的摘要 / 网络研习社#59 to:

Written by

Designer , Poet , Technology enthusiasts

Read more #cn posts


Best Posts From lemooljiang

We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From lemooljiang