jinni
jinni

Reputation: 373

Make a string a literal regex in ruby

I have an unknown input string, and I want to use it as a regex, but it can have many characters such as (,/,\

In Ruby, Is it possible to specify that the input argument to match is just a literal string.

e.g text.match_literal(string) instead of text.match(string) ?

Or, is the only way to do this by writing a function to backslash the problems?

Thankyou,

Upvotes: 0

Views: 855

Answers (1)

yazu
yazu

Reputation: 4660

You can use index method:

text.index(string)

Upvotes: 1

Related Questions