JavaScript | RegExp [^0-9] Expression

The RegExp [^0-9] Expression in JavaScript is used to search any digit which is not between the brackets. The character inside the brackets can be a single digit or a span of digits.

Syntax:

/[^0-9]/ 

or

new RegExp("[^0-9]")

Syntax with modifiers:

/[^0-9]/g 

or

new RegExp("[^0-9]", "g")

Example 1: This example searches the digits which are not present between [0-4] in the whole string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript RegExp [^0-9] Expression
    </title>
</head>
  
<body style="text-align:center">
      
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
      
    <h2>RegExp [^0-9] Expression</h2>
      
    <p>Input String: 1234567890</p>
      
    <button onclick="geek()">
        Click it!
    </button>
      
    <p id="app"></p>
      
    <script>
        function geek() {
            var str1 = "123456790";
            var regex4 = /[^0-4]/g;
            var match4 = str1.match(regex4);
  
            document.getElementById("app").innerHTML = 
                    "Found " + match4.length
                    + " matches: " + match4;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Clicking the button:
notdigit
After Clicking the button:
notdigit

Example 2: This example searches the digits which are not present between [0-9] in the whole string and replaces the characters with hash(#).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript RegExp [^0-9] Expression
    </title>
</head>
  
<body style="text-align:center">
      
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
      
    <h2>RegExp [^0-9] Expression</h2>
      
    <p>Input String: 128@$%</p>
      
    <button onclick="geek()">
        Click it!
    </button>
      
    <p id="app"></p>
      
    <script>
        function geek() {
           var str1 = "128@$%";            
           var replacement = "#";
            var regex4 = new RegExp("[^0-9]", "g");
            var match4 = str1.replace(regex4, replacement);
   
            document.getElementById("app").innerHTML =
                    "Found " + match4.length 
                    + " matches: " +  match4;
        }
    </script>
</body>
  
</html>

chevron_right


Output:
Before Clicking the button:
notdigit
After Clicking the button:
notdigit

Supported Browsers: The browsers supported by RegExp [^0-9] Expression are listed below:

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


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.