JavaScript RegExp multiline
Syntax & Examples
RegExp.multiline property
The multiline property of the RegExp object in JavaScript indicates whether the 'm' flag, which enables multiline searching, is set. This property is read-only.
Syntax of RegExp.multiline
The syntax of RegExp.multiline property is:
RegExp.prototype.multiline
This multiline property of RegExp whether or not to search in strings across multiple lines. This property is read-only.
Return Type
RegExp.multiline returns value of type Boolean
.
✐ Examples
1 Checking if the multiline property is enabled
In JavaScript, we can check if the 'm' flag is enabled for a RegExp object by accessing the multiline
property.
For example,
- We create a RegExp object
regex
with the 'm' flag/^abc/m
. - We check the
multiline
property ofregex
to see if it is true. - We log the result to the console.
JavaScript Program
const regex = /^abc/m;
const isMultilineEnabled = regex.multiline;
console.log(isMultilineEnabled);
Output
true
2 Comparing multiline property with and without the 'm' flag
In JavaScript, we can compare the multiline
property of RegExp objects with and without the 'm' flag.
For example,
- We create a RegExp object
regexWithM
with the 'm' flag/^abc/m
. - We create another RegExp object
regexWithoutM
without the 'm' flag/^abc/
. - We check the
multiline
property of both objects and log the results to the console.
JavaScript Program
const regexWithM = /^abc/m;
const regexWithoutM = /^abc/;
console.log(regexWithM.multiline); // true
console.log(regexWithoutM.multiline); // false
Output
true false
3 Using multiline property in conditional statements
In JavaScript, we can use the multiline
property in conditional statements to perform different actions based on whether the 'm' flag is enabled.
For example,
- We create a RegExp object
regex
with the 'm' flag/^abc/m
. - We check the
multiline
property ofregex
in anif
statement. - If the property is true, we log
'Multiline searching is enabled.'
to the console; otherwise, we log'Multiline searching is not enabled.'
.
JavaScript Program
const regex = /^abc/m;
if (regex.multiline) {
console.log('Multiline searching is enabled.');
} else {
console.log('Multiline searching is not enabled.');
}
Output
Multiline searching is enabled.
Summary
In this JavaScript tutorial, we learned about multiline property of RegExp: the syntax and few working examples with output and detailed explanation for each example.