首页 > 解决方案 > 如何从输出中删除完整的主题标签

问题描述

我正在使用 JavaScript 项目,Instafeed.js将 Instagram 个人资料输出到我的网站上。

该项目允许您使用 mustache 样式模板创建 HTML 模板。

我想从 Instagram 帖子的标题中删除所有主题标签。

这是我网页上的代码。在Instafeed.jsInstagram 供稿选项中,使用{{image}},来引用{{timestamp}}{{caption}}, ETC...

一切正常,直到我开始尝试使用正则表达式删除主题标签。

<script type="text/javascript">
    
    var feed = new Instafeed({
      accessToken: InstagramToken,
      limit: 10,
      target: 'instafeed2',
      transform: function(item) { //Transform receives each item as its argument
    // Over-write the original timestamp
    item.timestamp = new Date(item.timestamp).toLocaleString('en-AU', {
        weekday: 'long', 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric'
      });

    // return the modified item
    return item;
   
  },

  

transform: function(item) { //Transform receives each item as its argument
    // Over-write the original timestamp
    item.caption = new (item.caption),
        regexp = /\#\w\w+\s?/g,
        item.caption = item.caption.replace(regexp, '')
        
      

    // return the modified item
    return item;
  },
      template: '<div class="parent-container"><div class="post-title">{{timestamp}}</div><div class="vertical-line"></div> <div class="post-container">   <div class="post-image"><img src="{{image}}"/></div> <div class="post-description">{{caption}}</div>     </div>     <div class="vertical-line"></div>    </div>',
    });
    feed.run();
</script>

标签: javascript

解决方案


您可以使用此正则表达式:

const caption = 'asdasd #hashtag1 asdasdasd ascxzc.#hashtag2 asd asd asd #hashtag3,#hashtag4 asdads'
const regexp = new RegExp('#([^\\s]*)','g');
console.log(caption.replace(regexp, '').replace(/  +/g, ' '));

推荐阅读