从String中提取Hashtags

时间:2014-03-30 07:40:04

标签: ruby-on-rails ruby haml

假设我有一个带有一些主题标签的段落,我想将它们复制出来并单独显示它们。这是一些虚拟文本:

  

可视化如何启用#vertical #architectures?无论何时你   可以,解除垂直#convergence? #Visualize你怎么样   e-enable #vertical架构?无论何时,只要中断   垂直#convergence?

我想在我的模板上提供主题标签:#convergence,#Visualize,#vertical。

2 个答案:

答案 0 :(得分:11)

str = "Visualize how you can e-enable #vertical #architectures?..."
str.scan(/#\w+/).flatten #=> ["#vertical", "#architectures", "#convergence",...]

答案 1 :(得分:5)

使用正则表达式,

input = 'Visualize how you can e-enable #vertical #architectures? Whenever you can, disintermediate vertical #convergence? #Visualize how you can e-enable #vertical architectures? Whenever you can, disintermediate vertical #convergence?'
input.scan(/#(\w+)/)

会帮你的

[["vertical"], ["architectures"], ["convergence"], ["Visualize"], ["vertical"], ["convergence"]]