JavaScript Regex Dotall and Flags Properties

In JavaScript regular expressions, there are two properties related to flags for validation. The properties "dotAll" and "flags" within the object interact with the flag within the regular expression pattern. The dotAll property indicates whether the "s" flag is being used in the regular expression. On the other hand, the flags property displays the flags in the opposite order.

JavaScript regex dotall property

The JavaScript regex property "dotAll" indicates the presence of the "s" flag by returning a Boolean value. It returns true if the "s" flag is present and false if it is not.

Syntax

The syntax below is utilized for accessing the prototype of an object.

Example

regexObject.dotAll;

Explanation:

  • The dotAll property indicates whether the "s" flag is set in Boolean form, revealing its availability.
  • Examples

The provided instances demonstrate the functionality of the dotAll attribute in regular expressions and its corresponding value.

Example 1:

The example illustrates the regular expression format for the dotAll property. In this case, the regular expression is directly employed with the dotAll capability.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp dotAll property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 370px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp dotAll property

        </h3>

        <h4> The dotAll property gets the availability using the regex function

        </h4>

        <!-- Click the button to get the properties output. -->

        <button onclick="display_value();"> Click Here! </button>

        <div id="regex_dotall"> </div>

    </div>

    <script>

        function display_value() {

            //Use regex pattern with the "s" flag 

            let dotall_pattern = /javascript/s;

            // add dotAll property using regex pattern

            var final_result = dotall_pattern.dotAll;

            // get Boolean format output (true) in div tag 

            var ele_demo = document.getElementById("regex_dotall");

            ele_demo.innerHTML = "Available dotall property: " + final_result;

        }

    </script>

</body>

</html>

Output

The output reveals the actual value of the objective function.

Example 2:

The following example illustrates the regular expression format for the dotAll feature. In this demonstration, we apply the regular expression directly using the property syntax. Since the "s" flag is not supported, the result indicates a false value.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp dotAll property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp dotAll property

        </h3>

        <h4> The dotAll property gets the availability using the regex function

        </h4>

        <!-- Click the button to get the properties output. -->

        <button onclick="display_value();"> Click Here! </button>

        <div id="regex_dotall"> </div>

    </div>

    <script>

        function display_value() {

            //Use the required regex format

            let dotall_pattern = /^good$/;

            //use regex format for dotAll property

            var final_result = dotall_pattern.dotAll;

            //// get Boolean (format) in div tag 

            var ele_demo = document.getElementById("regex_dotall");

            ele_demo.innerHTML = "Available dotall property: " + final_result;

        }

    </script>

</body>

</html>

Output

The output displays the incorrect value of the object function.

Example 3:

The following example illustrates how to utilize the dotAll property in a regular expression pattern. In this case, we employ the regex syntax along with specific keywords to generate the desired output.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp constructor property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp dotAll property

        </h3>

        <h4> The dotAll property gets the availability using the regex function

        </h4>

        <!-- Click the button to get the properties output. -->

        <button onclick="display_value();"> Click Here! </button>

        <div id="regex_constructor"> </div>

    </div>

    <script>

        function display_value() {

            //use regex pattern with the s flag

            let input_str = new RegExp("a{2, }", "s");

            //use dotAll syntax with the object of the regex

            var final_result = input_str.dotAll;

            //Get the output in the div tag as a true

            var ele_demo = document.getElementById("regex_constructor");

            ele_demo.innerHTML = "Available constructor: " + final_result;

        }

    </script>

</body>

</html>

Output

The output displays the object function's native code.

JavaScript Regex Flags Property

You can access the flags used in Regular Expressions through the JavaScript RegExp flags property. The flags are concatenated into a string and returned in alphabetical order.

Syntax

The syntax below is utilized for retrieving the value of the object flag.

Example

regexObject.flags;

Explanation:

  • The flags display the result value using flag notation in increasing order.
  • In cases where the flag is not present and employs the "flags" attribute, the result will display as either blank or containing a space.
  • Examples

Below are some instances that demonstrate the functionality of the flags attribute in regular expressions and its corresponding value.

Example 1:

The provided illustration demonstrates the syntax for the flags property in regular expressions. In this scenario, we are utilizing the property along with the regular expression directly to obtain an output that is sorted alphabetically.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp flags property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        JavaScript RegExp flags property

        </h3>

        <h4> The flags property gets the available flags or modifiers using the regex function

        </h4>

        <!-- Click the button to get the properties output. -->

        <button onclick="display_value();"> Click Here! </button>

        <div id="regex_flags"> </div>

    </div>

    <script>

        function display_value() {

            //use required regex pattern

            let flags_pattern = /^[a-d]/sd;

            //use flags property with regex pattern object

            var final_result = flags_pattern.flags;

            //Get the sd as an output in the div tag

            var ele_demo = document.getElementById("regex_flags");

            ele_demo.innerHTML = "Available flags property: " + final_result;

        }

    </script>

</body>

</html>

Output

The displayed output presents the flag details in increasing order.

Example 2:

The demonstration illustrates the flags attribute utilized with regular expressions. In this case, the attribute is employed directly with the regular expression to retrieve alphabetically flagged information.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp flags property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp flags property

        </h3>

        <h4> The flags property gets the available flags or modifier using the regex function

        </h4>

        <!-- Click the button to get the properties output. -->

        <button onclick="display_value();"> Click Here! </button>

        <div id="regex_flags"> </div>

        <script>

            function display_value() {

                //use regex format with required flags 

                let flags_pattern = /^[a-d]/dm;

                //use flags property with regex pattern object

                var final_result = flags_pattern.flags;

                //get the dm as an output in the div tag

                var ele_demo = document.getElementById("regex_flags");

                ele_demo.innerHTML = "Available flags property: " + final_result;

            }

        </script>

</body>

</html>

Output

The displayed output presents the flag details in increasing order.

Example 3:

The following example demonstrates the utilization of the regex pattern method with the flags property. In this scenario, we apply the flags 'm' and 'i' along with the regex format to obtain the desired output.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp flags property

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp flags property

        </h3>

        <h4> The flags property gets the available flags or modifiers using the regex function

            </h4>

            <!-- Click the button to get the properties output. -->

            <button onclick="display_value();"> Click Here! </button>

            <div id="regex_constructor"> </div>

    </div>

    <script>

        function display_value() {

            //Use regex pattern with the mi flag

            let input_str = new RegExp("a{2, }", "mi");

            //use flags syntax with the object of the regex

            var final_result = input_str.flags;

            //Get the output in the div tag as a true

            var ele_demo = document.getElementById("regex_constructor");

            ele_demo.innerHTML = "Available constructor: " + final_result;

        }

    </script>

</body>

</html>

Output

The displayed results present the flag details in increasing order.

Conclusion

The property retrieves the regular expression pattern along with additional flags or modifiers.

Input Required

This code uses input(). Please provide values below: