I have an app where a user can input text in a textarea. I then use that to match against a large block of text to extract whatever the user entered in the textarea if found.
Problem is, if that textarea includes a | pipe, the it stops at the pipe:
Example:
User Input: This is my entry, I should all be remove | thank you very kindly
Larger block of text: blah blah blah blah blah This is my entry, I should all be remove | thank you very kindly
Desired return is: blah blah blah blah blah
But what I get back is:
bad: blah blah blah blah blah | thank you very kindly
The regex works like so:
desired = larger_block_of_text.sub(/#{user_input}/m, '').strip
Ideas? Thank you
You can use
Regexp.quote(user_input)to escape the special characters in the regular expression so that they are treated literally.Note: If you just want to find a constant string you don’t need regular expressions. You can instead use a String instead of a regular expression as the argument to
sub.