JavaScript multiline Property

The JavaScript multiline property is a read-only property of regular expression objects that returns true if the 'm' modifier (multiline flag) has been set, and false otherwise. The 'm' flag changes the behavior of ^ and $ anchors to match at line breaks within the string.

Syntax

regexp.multiline

Return Value

Returns a boolean value:

  • true if the 'm' flag is set
  • false if the 'm' flag is not set

Example: Checking multiline Property

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>JavaScript multiline Property</title>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
         margin: 20px;
      }
      .result {
         font-size: 18px;
         font-weight: 500;
         color: blue;
         margin: 10px 0;
      }
      button {
         background-color: #4CAF50;
         color: white;
         padding: 10px 20px;
         border: none;
         cursor: pointer;
         font-size: 16px;
      }
   </style>
</head>
<body>
   <h1>JavaScript multiline Property</h1>
   <div class="result" id="result1"></div>
   <div class="result" id="result2"></div>
   <button onclick="checkMultiline()">Check multiline Property</button>
   
   <script>
      function checkMultiline() {
         // Regular expression with 'm' flag
         let patternWithM = /start/gm;
         
         // Regular expression without 'm' flag
         let patternWithoutM = /start/g;
         
         document.getElementById("result1").innerHTML = 
            `Pattern with 'm' flag: multiline = ${patternWithM.multiline}`;
         
         document.getElementById("result2").innerHTML = 
            `Pattern without 'm' flag: multiline = ${patternWithoutM.multiline}`;
      }
   </script>
</body>
</html>

Output

When you click the button, you'll see:

Pattern with 'm' flag: multiline = true
Pattern without 'm' flag: multiline = false

Practical Example: Multiline Behavior

<!DOCTYPE html>
<html>
<head>
   <title>Multiline Flag Demonstration</title>
</head>
<body>
   <h2>Multiline Flag Demonstration</h2>
   <pre id="output"></pre>
   
   <script>
      const text = `Line 1: start of first line
Line 2: start of second line
Line 3: middle content`;

      // Without multiline flag
      const regexWithoutM = /^start/g;
      
      // With multiline flag  
      const regexWithM = /^start/gm;
      
      let output = "Text to search:
" + text + "

"; output += "Without 'm' flag (multiline = " + regexWithoutM.multiline + "):
"; output += "Matches: " + text.match(regexWithoutM) + "

"; output += "With 'm' flag (multiline = " + regexWithM.multiline + "):
"; output += "Matches: " + text.match(regexWithM); document.getElementById("output").textContent = output; </script> </body> </html>

Output

Text to search:
Line 1: start of first line
Line 2: start of second line  
Line 3: middle content

Without 'm' flag (multiline = false):
Matches: start

With 'm' flag (multiline = true):
Matches: start,start

Key Points

  • The multiline property is read-only and cannot be changed after regex creation
  • When multiline is true, ^ matches at the start of each line, not just the string
  • When multiline is true, $ matches at the end of each line, not just the string
  • Other regex flags like g (global) and i (ignore case) don't affect this property

Conclusion

The multiline property helps you determine whether a regular expression will treat ^ and $ anchors as line boundaries. This is essential when working with multi-line text processing where you need line-by-line pattern matching.

Updated on: 2026-03-15T23:18:59+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements