javascript - 如何从输出中删除完整的主题标签
问题描述
我正在使用 JavaScript 项目,Instafeed.js
将 Instagram 个人资料输出到我的网站上。
该项目允许您使用 mustache 样式模板创建 HTML 模板。
我想从 Instagram 帖子的标题中删除所有主题标签。
这是我网页上的代码。在Instafeed.js
Instagram 供稿选项中,使用{{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>
解决方案
您可以使用此正则表达式:
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, ' '));
推荐阅读
- java - 如何在 Android App Programmatic 中将 PDF 文件转换为 DOC 文件
- reactjs - 无法创建 React Typescript 应用程序
- .net - 使用powershell向目录NTFS权限添加条件
- python - 如何在 Lasso 和 RobustScalar 之后对回归预测进行逆变换?
- php - 如何构建一个虚拟表,其标题来自其他表的值
- python - 将文件从一个模型的 FileField 复制到另一个模型的 FileField 而不读取它
- java - 链式动画不适用于 AnimatorSet
- java - ajax 不会发出 http post 请求
- c++ - 不使用兼容工具集时删除预制配置
- ios - 仅在单元测试中对协议的冗余一致性