JavaScript RegExp 0 Metacharacter

In JavaScript, the regular expression "\0" is used to identify the presence of a null character within an input string. When a null character is found, the search method will return the position of the character. If the null character is not present, the search method will return "-1". This Metacharacter can be utilized for testing, searching, and matching specific strings as needed.

Syntax

The syntax is utilized to display the characters that are accessible at the beginning of a string.

Example

new RegExp("\\0")

Supported Browsers:

The RegExp \0 Metacharacter supports the following browsers supported using the JavaScript function:

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer
  • Examples

The provided instances demonstrate the utilization of JavaScript's regex metacharacter "\0" to search for, match, test, and execute null values.

Example 1

Below is an example demonstrating the use of the null character metacharacter in a regular expression in JavaScript. The test method evaluates whether a value is present and returns either true or false accordingly.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp \0 (null character) Metacharacter

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp \0 Metacharacter

        </h3>

        <h4> We can use the validation for the position of the null value in the input string.

        </h4>

        <div id="regex_information"></div>

        <!-- Click the button to get regex output -->

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

    </div>

    <script>

        function display() {

            //use available null pattern metacharacter of the regex

            let input_pattern = /\0/;

            var t1 = input_pattern.test('\0992JavaScript2');

            var t2 = input_pattern.test('Hii~.\0=@Err');

            var t3 = input_pattern.test('%!@!#$%');

            //The test availability shows Boolean value as an output

            var ele = document.getElementById("regex_information");

            ele.innerHTML = "\0992JavaScript2 : " + t1 + "<br> Hii~.\0=@Err : " + t2 + " <br> %!@!#$% : " + t3

        }

    </script>

</body>

</html>

Output

The output displays the non-alphanumeric character within the string.

Example 2

In the provided illustration, the regex in JavaScript is examined to determine the position of the null character metacharacter. If the null value is not found, the output will display "-1".

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp \0 (null character) Metacharacter

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp \0 Metacharacter

        </h3>

        <h4> We can use the validation for the position of the null value in the input string.

        </h4>

        <div id="regex_information"></div>

        <!-- Click the button to get regex output -->

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

    </div>

    <script>

        function display() {

            //use available null pattern metacharacter  

            let input_pattern = /\0/;

            var val1 = 'T point Tech\0';

            var val2 = '!!#HiiErr';

            var val3 = '\0%!@!#$%0';

            // use the regex search method for \0 Metacharacter

            var t1 = val1.search(input_pattern);

            var t2 = val2.search(input_pattern);

            var t3 = val3.search(input_pattern);

            // the search and get the position of the regex pattern as an output

            var ele = document.getElementById("regex_information");

            ele.innerHTML = "T point Tech\0 : " + t1 + "<br> !!#HiiErr : " + t2 + " <br> \0%!@!#$% : " + t3;

        }

    </script>

</body>

</html>

Output

The result displayed indicates the index of the null terminator within the string.

Example 3

Below is a demonstration illustrating the location of the null character metacharacter within a regular expression in JavaScript. In case the null value is not present, the output will be "null."

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp \0 (null character) Metacharacter

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>



<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp \0 Metacharacter

        </h3>

        <h4> We can use the validation for the position of the null value in the input string.

        </h4>

        <div id="regex_information"></div>

        <!-- Click the button to get regex output -->

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

    </div>

    <script>

        function display() {

            //use available null pattern metacharacter with match method

            let input_pattern = /\0/;

            var val1 = 'T point Tech\0';

            var val2 = '!!#HiiErr';

            var val3 = '\0%!@!#$%0';

            var t1 = val1.match(input_pattern);

            var t2 = val2.match(input_pattern);

            var t3 = val3.match(input_pattern);

            // the match regex pattern and shows array value as an output

            var ele = document.getElementById("regex_information");

            ele.innerHTML = "T point Tech\0 : " + t1 + "<br> !!#HiiErr : " + t2 + " <br> \0%!@!#$% : " + t3;

        }

    </script>

</body>

</html>

Output

The result indicates the location of the null terminator within the string.

Example 4

In this demonstration, the substitution of the null character metacharacter with the regex in JavaScript is illustrated. The null metacharacter "/0" is being replaced by the value "88" in this scenario.

Example

<!DOCTYPE html>

<html>

<head>

    <title>

        JavaScript RegExp \0 (null character) Metacharacter

    </title>

    <style>

        #demo1 {

            background-color: orange;

            border: 1px solid black;

            width: 280px;

        }

    </style>

</head>

<body>

    <div id="demo1">

        <h3>

            JavaScript RegExp \0 Metacharacter

        </h3>

        <h4> We can use the validation for the position of the null value in the input string.

        </h4>

        <div id="regex_information"></div>

        <!-- Click the button to get regex output -->

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

    </div>

    <script>

        function display() {

            //use available null pattern metacharacter of the regex

            let input_pattern = /\0/;

            var val1 = 'T point Tech\0';

            var val2 = '!!#HiiErr';

            var val3 = '\0%!@!#$%0';

            //use replace method with value

            var replace_var = '88';

            var t1 = val1.replace(input_pattern, replace_var);

            var t2 = val2.replace(input_pattern, replace_var);

            var t3 = val3.replace(input_pattern, replace_var);

            // the replace value and shows new values as an output

            var ele = document.getElementById("regex_information");

            ele.innerHTML = "T point Tech\0 : " + t1 + "<br> !!#HiiErr : " + t2 + " <br> \0%!@!#$% : " + t3;

        }

    </script>

</body>

</html>

Output

The result displays the substituted value using the existing null characters within the string.

Conclusion

Regular expressions are utilized in JavaScript to identify null characters within data. Metacharacters play a key role in validating user input effectively.

Input Required

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