user1179942
user1179942

Reputation: 391

Using regex with newline "\n"

Is it possible to use regex in spite of the newline character \n

e.g. this code works well

> "|text|".gsub(/\|(.+?)\|/){"###"}
=> "###"

this doesn't

> "|\n text|".gsub(/\|(.+?)\|/){"###"}
=> "|\n text|"

Upvotes: 1

Views: 131

Answers (2)

ohaal
ohaal

Reputation: 5268

Here is an alternative to the m modifier (specific to your case):

"|\n text|".gsub(/\|([^\|]+?)\|/){"###"}

It will match until the next |.

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31077

Use the m modifier for multiline matches:

"|\n text|".gsub(/\|(.+?)\|/m){"###"}

Upvotes: 3

Related Questions