Explain ‘dotAll’ flag for regular expressions in JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

The dotAll flag returns true or false depending upon if the s flag has been set in the regular expression or not.

Following is the code to implement dotAll flag for regular expressions in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en" >
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 20px;
      font-weight: 500;
<h1>dotAll flag for regular expressions</h1>
<div style="color: green;" class="result"></div>
<button class="btn">CLICK HERE</button>
Click on the above button to know if s flag has been set in regex or not
   let btnEle = document.querySelector(".btn");
   let resEle = document.querySelector(".result");
   const regex = new RegExp("Hello", "s");
   btnEle.addEventListener("click", () => {
      resEle.innerHTML = "The s flag has been set : " + regex.dotAll;


The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −

Published on 15-Jul-2020 16:38:14