Find digits not between the brackets using JavaScript Regular Expressions?


In JavaScript, the regular expression [^0-9] is used to find the characters inside the bracket but not a digit. Except digit mentioned in the bracket, it will return the remaining characters from the text as an array.

We all know about RegExp (Regular Expression) in JavaScript. RegExp is an object that specifies the pattern used to do a search and replace operations on the string or for input validation. RegExp was introduced in ES1 and it is fully supported by all browsers.

Syntax

Syntax for non-digit or /[^0-9]/ character is-

new RegExp("[^0-9]") or simply /[^0-9]/ 

Here /[^0-9]/ , is introduced in ES1. It is fully supported by all browsers. Like, as Chrome, IE, Safari, Opera, Firefox and Edge.

RegExp has modifiers like g, i, m. "g" for performing global matches, "i" for performing case-insensitive matching, and "m" for performing multiline matching.

Syntax for \w with modifier like,

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

Steps to find digits not between the digits

STEP 1 - Define a string containing digits.

STEP 2 - Define a regular expression pattern of digits not between the bracket.

STEP 3 - Match the string with the regex pattern using the match() method.

STEP 4 - Display the matched digits.

Now, let's see how to find characters inside the bracket except digits.

Example

In this example, we find the digits not between the bracket [0, 4].

<!DOCTYPE html>
<html>
<body>
   <h2>Digits not between the bracket</h2>
   <p>Digits not in the bracket [1, 4]:
      <p id = "result"></p>
   </p>
   <script>
      let text = "1234567890";
      let pattern = /[^1-4]/g;
      let result = text.match(pattern);
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>

Example

In the below example, we find the characters not between the bracket [1, 4].

<!DOCTYPE html>
<html>
<body>
   <h2>RegExp [^1-4]</h2>
   <p>Characters not in the bracket [1-4]:
      <p id = "result"></p>
   </p>
   <script>
      let text = "1234567890_Hello@$%^&65434320867042ecsceQW";
      let pattern = /[^1-4]/g;
      let result = text.match(pattern);
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>

Here, we can observe text we are given as 0-9 and "_Hello@$%^&". But in the pattern, we mentioned don't print numbers from [1-4]. So, match() method will return the remaining character as an array otherwise match() method will return as null. Let's see another example

Example

<!DOCTYPE html>
<html>
<body>
   <h2>JavaScript Regular Expressions</h2>
   <p id = "result"></p>
   <script>
      let text = "123456789";
      let pattern = /[^1-9]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML = "No characters found";
      } else {
         document.getElementById("result").innerHTML = "Characters are " + result;
      }
   </script>
</body>
</html>

Here, text is 1-9 and also patterns are not 1-9. So, match() method will not find any characters other than 1-9 from the text. So, it will return as null. Then, if the statement is executed. If we give text as shown in the first example, then else will be executed.

Example

<!DOCTYPE html>
<html>
<body>
   <h2>JavaScript Regular Expressions</h2>
   <p id = "result">Characters not between the bracket [1, 9]: <br><br> </p>
   <script>
      let text = "9876543210_Hello@$%^&";
      let pattern = /[^1-9]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML += "No characters found";
      } else {
         document.getElementById("result").innerHTML += "Characters are " + result;
      }
   </script>
</body>
</html>

Now, we will check how to replace word character(s) in a given text. To do this, we will use another regex of digits only. We match this regex with the previous result. Let’s see an example

Example

In the below example, we find the digits only not between the bracket [1, 4].

<!DOCTYPE html>
<html>
<body>
   <h1>RegExp [^0-9]</h1>
   <p>Digits not in the bracket [1,4]:
      <p id = "result"></p>
   </p>
   <script>
      let text = "1234567890_Hello@$%^&65434320867042ecsceQW";
      let pattern = /[^1-4]/g;
      let result = text.match(pattern);
      let pattern2 = /\d/g;
      document.getElementById("result").innerHTML =
      JSON.stringify(result).match(pattern2);
   </script>
</body>
</html>

In the above program, we first find all characters, not between the bracket [1, 4], and then find the digits only from this set of characters.

Updated on: 08-Dec-2022

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements