d vs D in JavaScript?



This article discusses about \d vs\ D in JavaScript regex. The \d and \D meta characters are used to match the characters of a given string. \d and \D are different from each other. \d (character) is equivalent to [0-9] that means it matches any single number.

The characters that are not matched by the \d are matched by the \D. \D is equivalent to [^0-9] that means it matches any character other than number. Both \d and \D are completely inverse to each other. To match a string with a pattern, the possible methods are: string.match(pattern), string.search(pattern), pattern.exec(string), pattern.test(text). We use match method only in these examples.

Syntax

The syntax for \d regex is −

RegExp("\d", "g")
Or
/\d/g

Example 1

This is an example program to illustrate the use of \d metacharacter.

<!DOCTYPE html>
<html>
<head>
   <title>\d in JavaScript regex</title>
</head>
<body style="text-align : center">
   <h3>\d in JavaScript regex</h3>
   <p id='result'></p>
   <script>
      var string = "1012 Brewery House, Road no. 24 Gachibowli, Hyderabad!"
      var reg_ex = new RegExp("\d", "g");
      var output = string.match(reg_ex);
      document.getElementById('result').innerHTML = output;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

The syntax for \D regex is −

RegExp("\D", "g")
Or
/\D/g

This is an example program to illustrate the use of \D metacharacter.

<!DOCTYPE html>
<html>
<head>
   <title>\d vs \D in JavaScript regex</title>
</head>
<body style="text-align : center">
   <h3>\D in JavaScript regex</h3>
   <p id='result'></p>
   <script>
      var string = "1012 Brewery House, Road no. 24 Gachibowli, Hyderabad!"
      var reg_ex = new RegExp("\D", "g");
      var output = string.match(reg_ex);
      document.getElementById('result').innerHTML = output;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

This is an example program to compare the result for \d and \D pattern and user defined patterns.

<!DOCTYPE html>
<html>
<head>
   <title>\d vs \D in JavaScript regex</title>
</head>
<body style="text-align : center">
   <h3>\d vs \D in JavaScript regex</h3>
   <p id='result'></p>
   <script>
      var string = "1012 Brewery House, Road no. 24 Gachibowli, Hyderabad!"
      var reg_ex1 = new RegExp("\d", "g"); //Existing Regular expression Metacharacter \d
      var reg_ex2 = /[0-9]/g; // User-defined pattern
      var reg_ex11 = /\D/g; //Existing Regular expression Metacharacter \D
      var reg_ex22 = /[^0-9]/g; // User-defined pattern
      var output1 = string.match(reg_ex1);
      var output2 = string.match(reg_ex2);
      var output11 = string.match(reg_ex11);
      var output22 = string.match(reg_ex22);
      document.getElementById('result').innerHTML = 'For \d :'+'<br/>'+' Using RegExp("\d", "g") : '+output1+'<br/>'+'Using /[0-9]/g : '+output2+'<br/>'+'For \W :'+'<br/>'+'Using /\D/g : '+output11+'<br/>'+'Using /[^0-9]/g : '+output22;
   </script>
</body>
</html>

On executing the above code, the following output is generated.


Advertisements