# Find digit with JavaScript RegExp.

In this tutorial, we will see how to find digits (0-9) using JavaScript RegExp. ASCII value for digits [0-9] starts from 48 to 57. If you want to print any ASCII value for a digit, 48 needs to be added to that digit.

We denote digits as \d in the regular expression. A RegExp is an object that specifies the pattern used to do a search and replace operations on the string or for input validation.

## Syntax

Following is the syntax for regular expression pattern of digit or \d character −

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


/\d/ is introduced in ES1. It is fully supported by all browsers. Like, such 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

The syntax for \d with a modifier like,

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


### Algorithm

• STEP 1 − Define a string having some digits.
• STEP 2 − Define the RegExp pattern for digits.
• STEP 3 − Apply match(pattern) on the above-defined string to find digits in the string.
• STEP 4 − Display the result- the digits.

Let’s look at some program examples for more clear understanding.

## Example 1

In the program below, we use string match(pattern) to find digits (\d) in the given string. We use RegExp pattern as /\d/g. The string match() method returns an array of digits in the string.

<!DOCTYPE html>
<html>
<body>
<h2>RegExp digit finding</h2>
<p id="text"> </p>
<p>Digit(s):
<span id = "result"></span>
</p>
<script>
let text = "200 students are passed out 250 in the class.";
document.getElementById("text").innerHTML = text;
let pattern = /\d/g;
let result = text.match(pattern);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>


Here, If digits exist in the given text then the match() method will return an array of digits, else it will return as null. Let's see another example.

## Example 2

In the program below, we take a string with no digits and try to find digits in the string. We use string match(pattern) to find digits (\d) in the given string. We use the RegExp pattern as /\d/g. See what our output looks like.

<!DOCTYPE html>
<html>
<body>
<h2>Finding digits using RegExp</h2>
<p id = "result"></p>
<script>
let text = "All students are qualified for the test";
let pattern = /\d/g;
let result = text.match(pattern);
if(result == null){
document.getElementById("result").innerHTML = "No digits found in the text.";
} else {
document.getElementById("result").innerHTML = result;
}
</script>
</body>
</html>


## Example 3

In the program below, we search for digits between 2 and 7. For this, we use /[2-7]/g as the regular expression pattern.

<!DOCTYPE html>
<html>
<body>
<h2>RegExp digit finding</h2>
<p id = "text"></p>
<p>Digit(s):
<span id = "result"></span>
</p>
<script>
let myStr = "200 students are passed out 250 in the class.";
document.getElementById("text").innerHTML = myStr;
let pattern = /[2-7]/g;
let result = myStr.match(pattern);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>


We can replace the digit character(s) using some of the ways. Let's see an example below

## Example 4

Finding and Replacing Digits

In the example below, we find and replace the digits with the space character. We use the split() and join() methods for this purpose.

<!DOCTYPE html>
<html>
<body>
<h2>Replace digit character(s)</h2>
<p id = "text"></p>
<p>After replacing the digit character(s):
<span id = "result"></span>
</p>
<script>
let myStr = "200 students are passed out 250 in the class.";
document.getElementById("text").innerHTML = myStr;
let pattern = /\d/g;
let result = myStr.split(/\d/g).join(" ")
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>


## Example 5

We will also check to replace digit character(s) in a simpler way. Like,

<!DOCTYPE html>
<html>
<body>
<h2>Replace digit character</h2>
<p id = "text"></p>
<p>After replacing the digit character(s):
<span id = "result"></span>
</p>
<script>
let myStr = "200 students are passed out 250 in the class.";
document.getElementById("text").innerHTML = myStr;
let pattern = /\d/g;
let result = myStr.replace(/\d/g , " ")
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>


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

Here, we can observe in the text, that there are no digits. So, match() will return as null. Hope this tutorial will give clarification to finding digits in the given text.

Updated on: 26-Aug-2022

368 Views