I have a regex to grab everything between "*"
:
str = "Donec sed odio dui. *Nullam id dolor id nibh ultricies vehicula ut*"
str.match(/\*(.*)\*/)[1]
I want the match to be able to include newline characters. How can I do it?
Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.
By default, a dot matches any character, except for the newline. That default can be changed to add matching the newline by using the single line modifier: for the entire regular expression with the /s modifier, or locally with (? s) (and even globally within the scope of use re '/s' ).
The \r metacharacter matches carriage return characters.
Example : [^abc] will match any character except a,b,c . [first-last] – Character range: Matches any single character in the range from first to last.
You'll want to use the m
option which allows the dot to match new lines:
Donec sed odio dui. *Nullam id dolor id
nibh ultricies vehicula ut*
regex str.match(/\*(.*)\*/m)[1]
Live example: http://www.rubular.com/r/11u9TreEOL
Your expression will capture the text between the first and last *
symbol, but if you want to capture all text between each set of *
then you'd want to make the .*
less greedy like
str.match(/\*(.*?)\*/m)[1]
Live example http://www.rubular.com/r/rBLOnwy3By
Or you could tell it to simply match all non *
characters like, note the m
option is not needed as a new line character would be matched by a the negated [^*]
character class:
str.match(/\*([^*]*)\*/)[1]
Live example http://www.rubular.com/r/dhQzZ58ZzM
Put the m
modifier after the regex like /\*(.*)\*/m
.
By the way, your regex can be improved to:
str[/(?<=\*).*(?=\*)/m]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With