JavaScript RegExp [0-9] Groups (not for number)

In JavaScript, regex groups are utilized to represent numbers that are not represented as characters within a string. Through the regex method, we can establish a numerical range or individual numbers to extract the desired character. The test, search, and match methods do not contain specific numbers or symbols for the characters within the brackets, like [^9]. It is possible to utilize a group of numbers, for example, [^0-9].

Syntax

The syntax provided is utilized to create regex groups for numbers that are not available.

Example

/[^0-9]/
Example

/[^0-9]/modifier;

Explanation:

By employing the regex group, it is possible to make essential numbers inaccessible by utilizing a global modifier.

The regex group can be applied with or without a modifier, providing flexibility in its usage.

Examples

The illustrations below demonstrate the regex group [^0-9] or [^0] for identifying non-numeric characters within a string.

Example 1

An illustration demonstrating the utilization of a regex group for a basic number (^8) is presented below. In this scenario, a solitary number is employed alongside a global modifier.

Example

<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript RegExp groups
    </title>
    <style>
        #demo1 {
            background-color: orange;
            border: 1px solid black;
            width: 370px;
        }
    </style>
</head>

<body>
    <div id="demo1">
        <h3>
            JavaScript RegExp groups for numbers
        </h3>
        <h4> In the regex group test, the 8 number does not act as a character
        </h4>
        <div id="regex_group"></div>
        <!-- click the button to operate regex number group -->
        <button onclick="display_num();"> Click Here! </button>
    </div>
    <script>
        function display_num() {
            // use regex number group for number with global modifier
            let input_pattern = /[^8]/g;
            var group_t1 = input_pattern.test('99884532');
            var group_t2 = input_pattern.test('dfb003sbbff');
            var group_t3 = input_pattern.test('jf8838g');
            //Output shows the availability of the regex value in the string 
            var ele = document.getElementById("regex_group");
            ele.innerHTML = "99884532 : " + group_t1 + "<br> dfb003sbbff : " + group_t2 + " <br> jf888g : " + group_t3
        }
    </script>
</body>

</html>

Output

The result presents 8 numbers that are not present within the parentheses for the given string.

Example 2

The illustration below demonstrates the exclusion of the simple number group [^0-5] through the utilization of JavaScript regex. In this case, we specify numbers ranging from zero to five along with the modifier for numerical value.

Example

<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript RegExp groups
    </title>
    <style>
        #demo1 {
            background-color: orange;
            border: 1px solid black;
            width: 370px;
        }
    </style>
</head>

<body>
    <div id="demo1">
        <h3>
            JavaScript RegExp groups for numbers
        </h3>
        <h4> In the regex group test, the 0-5 number does not act as a character
        </h4>
        <div id="regex_group">
        </div>
        <!-- click the button to operate regex number group -->
        <button onclick="display_num();"> Click Here! </button>
    </div>
    <script>
        function display_num() {
            // use regex number group for 0 to 5 numerical pattern
            let input_pattern = /[^0-5]/g;
            var group_t1 = input_pattern.test('99884532');
            var group_t2 = input_pattern.test('dfb003sbbff');
            var group_t3 = input_pattern.test('jf888g');
            //Output shows the availability of the 0-5 number of group
            var ele = document.getElementById("regex_group");
            ele.innerHTML = "99884532 : " + group_t1 + "<br> dfb003sbbff : " + group_t2 + " <br> jf888g : " + group_t3
        }
    </script>
</body>
</html>

Output

The displayed Output indicates the range of numbers from 0 to 5 that are not available within the brackets for the specific string.

Example 3

In this instance, the given pattern is used to identify numbers outside the range specified within a string. The pattern [^0-5] is employed for this purpose.

Example

<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript RegExp groups
    </title>
    <style>
        #demo1 {
            background-color: orange;
            border: 1px solid black;
            width: 370px;
        }
    </style>
</head>
<body>
    <div id="demo1">
        <h3>
            JavaScript RegExp groups not for numbers
        </h3>
        <div id="regex_group">
        </div>
        <!-- click the button to operate regex number group -->
        <button onclick="display_num();"> Click Here! </button>
    </div>
    <script>
        function display_num() {
            // use regex number group for a numerical pattern with a modifier
            let input_pattern = /[0-5]/g;
            var group_input1 = "99884532";
            var group_input2 = "dfb003sbbff";
            var group_input3 = "998877";
            var group_t1 = group_input1.match(input_pattern);
            var group_t2 = group_input2.match(input_pattern);
            var group_t3 = group_input3.match(input_pattern);
            //Output shows the array value of the regex group pattern 
            var ele = document.getElementById("regex_group");
            ele.innerHTML = "99884532 : " + group_t1 + "<br> dfb003sbbff : " + group_t2 + " <br> 998877 : " + group_t3
        }
    </script>
</body>
</html>

Output

The result displayed in the bracket indicates the numbers between 0 and 5 that are not available in the string.

Example 4

In the illustration below, a search is conducted within a specified set of numbers to determine the position of a particular number. This search operation indicates whether the number is present within the collection.

Example

<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript RegExp groups
    </title>
    <style>
        #demo1 {
            background-color: orange;
            border: 1px solid black;
            width: 370px;
        }
    </style>
</head>
<body>
    <div id="demo1">
        <h3>
            JavaScript RegExp groups not for numbers
        </h3>
        <h4> The regex group searches the not in [0-5] numbers as a character
        </h4>
        <div id="regex_group"></div>
        <!-- click the button to operate regex number group -->
        <button onclick="display_num();"> Click Here! </button>
    </div>
    <script>
        function display_num() {
            // use regex number group for numerical pattern
            let input_pattern = /[^0-5]/g;
            var group_input1 = "99884532";
            var group_input2 = "dfb003sbbff";
            var group_input3 = "998877";
            var group_t1 = group_input1.search(input_pattern);
            var group_t2 = group_input2.search(input_pattern);
            var group_t3 = group_input3.search(input_pattern);
            //Output shows the position of the pattern value in the string 
            var ele = document.getElementById("regex_group");
            ele.innerHTML = "99884532 : " + group_t1 + "<br> dfb003sbbff : " + group_t2 + " <br> 998877 : " + group_t3
        }
    </script>
</body>
</html>

Output

The output indicates that the number is not available.

Example 5

An illustration below demonstrates the absence of a particular group's value. It employs the match function to locate the group (^123) within a string. The regular expression extracts the first, second, and third digits from the input string.

Example

<html>
<head>
    <title>
        JavaScript RegExp groups
    </title>
    <style>
        #demo1 {
            background-color: orange;
            border: 1px solid black;
            width: 370px;
        }
    </style>
</head>

<body>
    <div id="demo1">
        <h3>
            JavaScript RegExp groups for numbers
        </h3>
        <div id="regex_group"></div>
        <!-- click the button to operate regex number group -->
        <button onclick="display_num();"> Click Here! </button>
    </div>
    <script>
        function display_num() {
            //Use number regex pattern for a set of numbers
            let input_pattern = /[^123]/g;
            var group_input1 = "0199884532";
            var group_input2 = "dfb003sbbff";
            var group_input3 = "9298877";
            var group_t1 = group_input1.match(input_pattern);
            var group_t2 = group_input2.match(input_pattern);
            var group_t3 = group_input3.match(input_pattern);
            var ele = document.getElementById("regex_group");
            //Output shows the matching value in the string 
            ele.innerHTML = "0199884532 : " + group_t1 + "<br> dfb003sbbff : " + group_t2 + " <br> 9298877 : " + group_t3
        }
    </script>
</body>
</html>

Output

The result displays a set of numbers within a string.

Conclusion

The regular expression for number groups is employed to verify numeric values by utilizing square brackets with numbers [0-9].

Input Required

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