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.dotAll

This 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,

  1. We create a RegExp object regex with the 's' flag /abc/s.
  2. We check the dotAll property of regex to see if it is true.
  3. 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,

  1. We create a RegExp object regexWithS with the 's' flag /abc/s.
  2. We create another RegExp object regexWithoutS without the 's' flag /abc/.
  3. We check the dotAll property 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); // false

Output

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,

  1. We create a RegExp object regex with the 's' flag /abc/s.
  2. We check the dotAll property of regex in an if statement.
  3. 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.