Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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:
-
trueif the 'm' flag is set -
falseif 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
multilineproperty is read-only and cannot be changed after regex creation - When
multilineistrue,^matches at the start of each line, not just the string - When
multilineistrue,$matches at the end of each line, not just the string - Other regex flags like
g(global) andi(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.
Advertisements
