user1134991
user1134991

Reputation: 3103

How can I have a full or in Ruby regular expression?

I want to check if a non letter/number or \W is at the beginning or the end of a variable, just a true or false. The usual code that I would use would be:

str.match( /^\W) || str.match( /\W$/ )

Of course, one could accomplish that using mutltiple ways, such as:

[ /^\W/, /\W$/ ].index{ | regy | str.match( regy ) }

however, I would like to know if there is a way to this in one regular expression. I.e.

str.match( regy ) # the single regexp handles the or part on it's own.

Thanks

Upvotes: 2

Views: 37

Answers (1)

hjpotter92
hjpotter92

Reputation: 80649

Yes. Use the | in regex:

/^\W|\W$/

Upvotes: 3

Related Questions