User input + Ruby + Regex?

I want to provide users with a text area where they can enter text. Later I will match this input with another input and pull it out if it matches.

Flow:

  • User enters text with returns in the text area
  • The text is saved in db in the text box

Then I extract the following:

text_reply = text_reply.sub(/#{user.text_to_extract}/m, '').strip 

The problem is that it seems like characters like new lines or pipes | violate it. Since the input we want to map might look like this:

 XXXXXX XXXXXX XXXX & XXXXX asdasd: 123312321 | dasasddsadasads http://yahoo.com 

Suggestions? Thansk

+7
source share
1 answer

You need to avoid typing: http://www.ruby-doc.org/core/classes/Regexp.html

See the escape method.

  Regexp.escape(your_input) 
+13
source

All Articles