![]() |
VOOZH | about |
The m modifier in JavaScript regular expressions stands for "multiline". It alters the behavior of the ^ (caret) and $ (dollar) anchors, allowing them to match the start or end of any line in a multiline string, rather than just the start or end of the entire string.
[ 'hello', index: 6, input: 'world\nhello\nJavaScript', groups: undefined ]
The ^hello pattern matches "hello" at the beginning of the second line because the m modifier treats each line separately.
let regex = /pattern/m;[ 'error', index: 25, input: 'info: everything is fine\nerror: something went wrong\ninfo: all good', groups: undefined ]
The ^error pattern matches "error" at the beginning of the second line.
[ 'fine', index: 20, input: 'info: everything is fine\nerror: something went wrong', groups: undefined ]
The $fine pattern matches "fine" at the end of the first line.
[ 'line1', 'line2', 'line3' ]
The ^\w+ pattern matches the first word in each line because of the m modifier.
Valid log format.
The ^(error|info): pattern ensures each line begins with "error:" or "info:".
line1 line2
The pattern ^\s*$ matches empty lines in the string, and the m modifier ensures all lines are checked.
/^word/m/^\s*$/m/\.$/m/^(info|error|debug):/mstr.split(/\r?\n/);The m modifier is an invaluable tool when working with multi-line strings in JavaScript. It ensures efficient line-by-line pattern matching and processing.