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.multilineThis 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
regexwith the 'm' flag/^abc/m. - We check the
multilineproperty ofregexto 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
regexWithMwith the 'm' flag/^abc/m. - We create another RegExp object
regexWithoutMwithout the 'm' flag/^abc/. - We check the
multilineproperty 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); // falseOutput
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
regexwith the 'm' flag/^abc/m. - We check the
multilineproperty ofregexin anifstatement. - 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.