| by Arround The Web | No comments

What Does the RegExp D Metacharacter Do in JavaScript

The “\D” RegExp metacharacter helps in finding the characters that do not lie in the digits range “0-9”. It applies the search based on the regex pattern and returns the strings as well as the special characters as its standard output. It also helps to find characters other than digits from the initialized string at the first match or from the whole string globally.

This write-up demonstrates the working of the “\D” metacharacter in the RegExp of JavaScript.

What Does the RegExp \D Metacharacter Do in JavaScript?

The “\D” metacharacter finds the non-digit characters in JavaScript. It works on the initialized regexp pattern and returns the non-digit characters present inside it.

Syntax (Basic)

/\D/

In the above syntax:

  • /(Forward-slash): Indicates the defined regular expression boundaries.
  • \(Backslash): Removes the backslash for treating the specified character as a metacharacter.
  • D: It refers to the metacharacter that matches the non-digit characters.

Syntax (With Modifiers)

/\D/[g,i,m] OR new RegExp("\\D", "[g,i,m")

In this syntax:

  • g(global): Searches the initialized pattern globally and does not stop after the first match.
  • i(case-sensitive): Matches character by ignoring the case sensitivity.
  • m(multiple): Specifies the multiline search and is limited between “^(start of the string)” and “$(end of the string)”.

Syntax (With RegExp() Constructor)

new RegExp("\\D")

In this syntax:

  • new: A keyword act as an operator for the creation of an object from a constructor.
  • RegExp(): Denotes the constructor that takes the “\\D” metacharacter as its parameter/argument.

Let’s implement the above syntaxes one by one to demonstrate the practical implementation of the “\D” metacharacter.

HTML Code

First, have a look at the following HTML code:

<h2>Find Digits Using \D Metacharacter</h2>
<p> String: Linux12345@...% </p>
<button ondblclick="search()">Double Click</button>
<p id="find"></p>

In the above code snippet:

  • Define a subheading via the “<h2>” tag.
  • Next, the “<p>” tag specifies the initialized string that needs to be evaluated.
  • After that, a button is added with the help of the “<button>” tag with an “ondblclick” event. It redirects the specified event to the function “search()” which will be triggered upon the button double-click.
  • Lastly, the “<p>” tag includes an empty paragraph with an assigned id “find” to display the matched non-digit characters.

Note: This HTML code will be followed throughout this write-up.

Example 1: Use the “\D” Metacharacter to Find Non-Digit Characters Based on Modifier Syntax (/\D/g)

This example utilizes the “/\D” metacharacter to search the non-digit characters globally using an additional “g(global)” modifier.

JavaScript Code

Consider the stated JavaScript code:

<script>
function search() {
var string = "Linux12345@...%";
var pattern = /\D/g;
var result = string.match(pattern);
document.getElementById("find").innerHTML = result;
}
</script>

In the above code block:

  • Define a function named “search()”.
  • The function definition initializes the stated “string” comprising the alphanumeric value.
  • Next, the variable “pattern” specifies the “/\D/g” metacharacter to search the non-digit characters globally from the initialized string.
  • After that, apply the “match()” method to match the non-digit characters using the “D” metacharacter specified in the “pattern” variable.
  • Lastly, the “getElementById()” method approaches the included empty paragraph via its id “find” to append and display the searched non-digit characters via the “innerHTML” property.

Output

The output returns the matched “non-digit characters” from the initialized string upon button double-click.

Example 2: Use the “\D” Metacharacter to Find Non-Digit Characters Based on “RegExp() Constructor” Syntax

This example uses the “new RegExp(\\D, g)” constructor syntax to show the working of the “\D” metacharacter practically:

JavaScript Code

Follow the given JavaScript code:

<script>
function search() {
var string = "Linux12345@...!";
var pattern = new RegExp("\\D", "g");
var result = string.match(pattern);
document.getElementById("find").innerHTML = result;
}
</script>

The above code lines, apply the “new RegExp(\\D, g)” syntax to globally match the non-digit characters from the initiated string at the button double-click.

Output

As analyzed, the outcome of “new RegExp(“//D”, “g”)” is identical to the “/\D/g”.

Conclusion

In JavaScript, the “\D” metacharacter in the regexp pattern finds the non-digit characters from the string and returns them. It stops the matching process at the first digit match by default without completing the search from the whole string. However, the “g(global)” modifier can be used to search the string globally based on the pattern. It is opposite to the lowercase “\d” metacharacter that matches the digits instead. This post provided the working and functionality of the RegExp “\D” metacharacter in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply