开发者

Filter through text and return the strings that start with #?

I currently have a te开发者_如何学Goxt form for users to post updates (similar to Twitter), how can I filter through the text thats submitted and return the full strings that start with # (e.g. if a user posts "Hello World! #noob #ruby", I would get #noob and #ruby back) I will likely save the results in a tag db column. Thank you very much for your help!


You can extract pattern-matching substrings out of a larger string using String#scan

# for example, to grab all characters after \# until the next whitespace
"Hello World! #noob #ruby".scan(/\#\S+/) # => ["#noob", "#ruby"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜