Find the non-digit character with JavaScript Regular Expression


In this tutorial, we will learn to find the non-digit character with JavaScript regular expression. ASCII code 'A' is 65 and 'a' is 97 etc. Now, we will check how to find a nondigit element (\D) in a given text using RegExp.

RegExp is an object that specifies the pattern used to do search and replace operations on string or for input validation. RegExp was introduced in ES1 and it is fully supported by all browsers.

Syntax

Syntax for the non-digit element is,

new RegExp("\D") or simply /\D/

/\D/, is introduced in ES1. It is fully supported by all browsers. Like, 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 \D with modifier like,

new RegExp("\D", "g") or simply /\D/g

Steps to find the non-digit character

STEP 1 - Define a string with some non-digit characters.

STEP 2 - Define a regular expression to find digits only.

STEP 3 - Perform matching between the string and regex.

STEP 4 - Display the result, i.e., non-digit characters.

Example

In the example below, we find non-digit characters using the match() method and regular expression in JavaScript.

<!DOCTYPE html>
<html>
<body>
   <h2>Finding non-digit characters</h2>
   <p>Non-digits characters:
      <p id = "result"></p>
   </p>
   <script>
      let text = "200 students qualified in the exam out of 250";
      let pattern = /\D/g;
      let result = text.match(pattern);
      document.getElementById("result").innerHTML=result;
   </script>
</body>
</html>

Here, If non-digits exist in a given text, match() method will return array of non-digits or it will return as null. Based on output from the match() method, we can perform operations. Like,

Example

In the example below, we define a string without any non-digit characters and try to find the non-digit chars in the string.

<!DOCTYPE html>
<html>
<body>
   <h1>Finding non-digit element</h1>
   <p id = "result"></p>
   <script>
      let text = "200250";
      let pattern = /\D/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML= "Sorry, No non-digits are found in the text";
      }else{
         document.getElementById("result").innerHTML= "Non-digit characters are: " + result;
      }
   </script>
</body>
</html>

For example, in the input text there are no non-digit characters. So, if the statement is executed. If input text has non-digit characters, the other part will be executed.

Example

Now, we will check how to replace the non-digit character(s). Let’s see an example

<!DOCTYPE html>
<html>
<body>
   <h1>Replace non-digit character(s)</h1>
   <p>After replacing non-digit character(s):
      <p id = "result"></p>
   </p>
   <script>
      let text = "200 students qualified in the exam out of 250";
      let pattern = /\D/g;
      let result = text.split(/\D/g).join(" ");
      document.getElementById("result").innerHTML=result;
   </script>
</body>
</html>

Example

We will also check another way to replace non-digit character(s). Let’s see an example

<!DOCTYPE html>
<html>
<body>
   <h1>Replace non-digit character(s)</h1>
   <p>After replacing non-digit character(s):
      <p id = "result"></p>
   </p>
   <script>
      let text = "200 students qualified in the exam out of 250";
      let pattern = /\D/g;
      let result = text.replace(/\D/g , " ");
      document.getElementById("result").innerHTML=result;
   </script>
</body>
</html>

As we discussed, g for global matches. Instead of stopping with the first occurance, it will look for all the occurrences.

Hope this article will help you give knowledge on finding non-digit characters using RegExp in JavaScript.

Updated on: 08-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements