简体   繁体   中英

How to check if a string contains certain characters in Ruby

I am trying to get Ruby to search a text for certain characters, such as # and $ . I then want to capture the character and the text beside it until there is white space, so if I would put this as text:

Hey, I am a #string and #what are you?

It would return the index at where the #string and #what are. This line only returns if the text has a special character:

<div class='tweet-text'>                
    <% if feed.text.match(/#\w+/) %>
        Hi
    <% end %>
</div>

The point here is I want to color the "#" word to a different color. The code is inside ejs. How would I do this?

When you want to highlight or mark one or more phrases in a text then you might want to have a look at the highlight view helper:

For example

<%= highlight('Hey, I am a #string and #what are you?', /#\w+/) %>

will return

Hey, I am a <mark>#string</mark> and <mark>#what</mark> are you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM