W3docs

JavaScript Regex m Flag (Multiline)

The m flag in JavaScript regular expressions allows the ^ and $ anchors to match the start and end of each line within a multi-line string, rather than just the

Introduction to Multiline Mode

The m flag in JavaScript regular expressions allows the ^ and $ anchors to match the start and end of each line within a multi-line string, rather than just the start and end of the entire string. This is particularly useful when working with multi-line text where you need to perform line-by-line pattern matching.

The m Flag: Enabling Multiline Mode

When you use the m flag, the ^ and $ anchors match the positions before and after line breaks within the string.

Using the m Flag


javascript— editable

In this example, the ^abc regex matches the start of each line that begins with "abc" due to the m flag. The g flag ensures all occurrences are matched. Without the m flag, it would only match "abc" at the very start of the string.

Example: Matching Multiple Lines


javascript— editable

Here, the regex ^start matches the beginning of lines that start with "start" due to the combination of the m and g flags.

Practical Applications

Matching Lines in Multi-line Text

The m flag is particularly useful for matching specific patterns at the start or end of each line in a multi-line string.


javascript— editable

This example shows how ^hello with the m flag matches "hello" at the start of each line.

Matching End of Lines in Multi-line Text

Similarly, you can use the m flag to match patterns at the end of each line.


javascript— editable

Here, world$ with the m flag matches "world" at the end of each line.

Example: Extracting Specific Lines


javascript— editable

This example demonstrates how to extract all lines that start with "Error:" from a log file using the m flag.

Combining Flags

You can combine the m flag with other flags to enhance your regular expressions. For instance, combining the m flag with the case-insensitive i flag allows you to perform case-insensitive multi-line matching.

Example: Case-Insensitive Multi-line Matching


javascript— editable

In this example, the regex matches lines containing "hello" regardless of case, due to the combination of the m and i flags.

  • The regex pattern ^.*hello.*$ matches any line that contains "hello" regardless of case.
  • The m flag ensures that each line in the multi-line string is treated individually.
  • Hello world, hello world, and HELLO WORLD all match the pattern because they contain "hello" (case-insensitive) somewhere in the line.
Info

Use the m flag in JavaScript regex to apply ^ and $ anchors to each line in a multi-line string, enhancing your ability to validate and manipulate line-by-line content.

Use the m Flag for Line-by-Line Validation

The m flag is ideal for scenarios where you need to validate or manipulate each line individually within a multi-line string.

Example: Validating a U.S. Phone Number


javascript— editable

This regex ensures that the phone number is in the format (123) 456-7890. It uses ^ to assert the start and $ to assert the end, ensuring that the entire string matches the specified pattern for each line.

So, both "(123) 456-7890" and "(234) 567-8901" match the regex pattern because they follow the format of (xxx) xxx-xxxx, which the regex is designed to match.

Conclusion

The m flag in JavaScript regular expressions extends the functionality of the ^ and $ anchors to match the start and end of each line in a multi-line string. This capability is invaluable for processing multi-line text data, enabling precise line-by-line pattern matching and validation.

Practice

Practice

What is the function of the 'm' flag in JavaScript regular expressions?