JavaScript RegExp dotAll
Syntax & Examples
RegExp.dotAll property
The dotAll property of the RegExp object in JavaScript indicates whether the 's' flag, which allows the dot (.) to match newline characters, is set. This property is read-only.
Syntax of RegExp.dotAll
The syntax of RegExp.dotAll property is:
RegExp.prototype.dotAllThis dotAll property of RegExp whether . matches newlines or not. This property is read-only.
Return Type
RegExp.dotAll returns value of type Boolean.
✐ Examples
1 Checking if the dotAll property is enabled
In JavaScript, we can check if the 's' flag is enabled for a RegExp object by accessing the dotAll property.
For example,
- We create a RegExp object
regexwith the 's' flag/abc/s. - We check the
dotAllproperty ofregexto see if it is true. - We log the result to the console.
JavaScript Program
const regex = /abc/s;
const isDotAllEnabled = regex.dotAll;
console.log(isDotAllEnabled);Output
true
2 Comparing dotAll property with and without the 's' flag
In JavaScript, we can compare the dotAll property of RegExp objects with and without the 's' flag.
For example,
- We create a RegExp object
regexWithSwith the 's' flag/abc/s. - We create another RegExp object
regexWithoutSwithout the 's' flag/abc/. - We check the
dotAllproperty of both objects and log the results to the console.
JavaScript Program
const regexWithS = /abc/s;
const regexWithoutS = /abc/;
console.log(regexWithS.dotAll); // true
console.log(regexWithoutS.dotAll); // falseOutput
true false
3 Using dotAll property in conditional statements
In JavaScript, we can use the dotAll property in conditional statements to perform different actions based on whether the 's' flag is enabled.
For example,
- We create a RegExp object
regexwith the 's' flag/abc/s. - We check the
dotAllproperty ofregexin anifstatement. - If the property is true, we log
'Dot matches newlines.'to the console; otherwise, we log'Dot does not match newlines.'.
JavaScript Program
const regex = /abc/s;
if (regex.dotAll) {
console.log('Dot matches newlines.');
} else {
console.log('Dot does not match newlines.');
}Output
Dot matches newlines.
Summary
In this JavaScript tutorial, we learned about dotAll property of RegExp: the syntax and few working examples with output and detailed explanation for each example.